:root {
    /* Design tokens */
    --bg: #f5f7f2;
    --bg-strong: #183f31;
    --surface: #ffffff;
    --surface-soft: #f8faf6;
    --surface-tint: #edf4ef;
    --text: #17251f;
    --muted: #627267;
    --line: #d9e3d8;
    --line-strong: #c4d2c4;
    --primary: #276749;
    --primary-strong: #1f513a;
    --accent: #c18a38;
    --accent-soft: #fff4df;
    --danger: #a33a32;
    --danger-soft: #fff0ef;
    --radius: 8px;
    --shadow: 0 18px 42px rgba(28, 52, 39, 0.12);
    --shadow-soft: 0 10px 26px rgba(28, 52, 39, 0.08);
}

/* Reset & document */
* {
    box-sizing: border-box;
}

html {
    background: var(--bg);
}

body {
    margin: 0;
    min-height: 100vh;
    background:
        linear-gradient(180deg, rgba(24, 63, 49, 0.08) 0, rgba(24, 63, 49, 0) 240px),
        linear-gradient(120deg, rgba(193, 138, 56, 0.08) 0, rgba(193, 138, 56, 0) 38%),
        var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    line-height: 1.5;
    position: relative;
}

body::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    background-image:
        linear-gradient(rgba(24, 63, 49, 0.035) 1px, transparent 1px),
        linear-gradient(90deg, rgba(24, 63, 49, 0.035) 1px, transparent 1px);
    background-size: 44px 44px;
    mask-image: linear-gradient(180deg, rgba(0, 0, 0, 0.58), transparent 58%);
    z-index: 0;
}

/* App shell */
.app-header,
.app-main,
.toast-stack {
    position: relative;
    z-index: 1;
}

.app-header {
    width: min(1240px, calc(100% - 32px));
    margin: 16px auto 0;
    padding: 10px 12px;
    border: 1px solid rgba(255, 255, 255, 0.16);
    border-radius: var(--radius);
    background: rgba(24, 63, 49, 0.96);
    box-shadow: 0 14px 34px rgba(20, 44, 33, 0.18);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    z-index: 30;
}

/* Brand & navigation */
.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1 1 auto;
    min-width: 0;
}

.brand-wrap {
    display: grid;
    grid-template-columns: 38px minmax(132px, max-content);
    align-items: center;
    gap: 10px;
    color: #f6fff9;
    flex: 0 0 auto;
}

.brand-mark {
    width: 38px;
    height: 38px;
    border: 1px solid rgba(255, 255, 255, 0.22);
    border-radius: var(--radius);
    background: linear-gradient(145deg, #f8fbf5 0%, #dbe9dc 100%);
    color: var(--primary-strong);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: inset 0 -1px 0 rgba(24, 63, 49, 0.12);
}

.brand-mark svg {
    width: 22px;
    height: 22px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.brand {
    display: block;
    color: #fbfff9;
    font-size: 0.94rem;
    font-weight: 800;
    letter-spacing: 0;
}

.brand-sub {
    display: block;
    margin-top: 1px;
    color: rgba(238, 250, 239, 0.72);
    font-size: 0.76rem;
    font-weight: 600;
}

.top-nav {
    display: flex;
    align-items: center;
    gap: 4px;
    flex: 0 1 auto;
    min-width: 0;
    flex-wrap: nowrap;
}

.nav-toggle {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    width: 40px;
    height: 40px;
    padding: 0;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: var(--radius);
}

.nav-toggle:hover {
    background: rgba(255, 255, 255, 0.16);
    border-color: rgba(255, 255, 255, 0.32);
}

.nav-toggle span {
    display: block;
    width: 18px;
    height: 2px;
    margin: 0 auto;
    border-radius: 999px;
    background: #f7fff8;
    transition: transform 0.15s ease, opacity 0.15s ease;
}

.nav-toggle[aria-expanded="true"] span:nth-child(1) {
    transform: translateY(6px) rotate(45deg);
}

.nav-toggle[aria-expanded="true"] span:nth-child(2) {
    opacity: 0;
}

.nav-toggle[aria-expanded="true"] span:nth-child(3) {
    transform: translateY(-6px) rotate(-45deg);
}

.nav-link,
.profile-chip {
    color: rgba(250, 255, 249, 0.82);
    text-decoration: none;
    padding: 8px 10px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    font-size: 0.84rem;
    font-weight: 700;
    line-height: 1;
    white-space: nowrap;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.nav-link:hover,
.profile-chip:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.14);
    color: #ffffff;
}

.nav-link.active,
.profile-chip.active {
    background: #f8fbf5;
    border-color: rgba(255, 255, 255, 0.28);
    color: var(--primary-strong);
}

.nav-link {
    flex: 0 0 auto;
    padding-left: 8px;
    padding-right: 8px;
}

.nav-dropdown {
    position: relative;
    flex: 0 0 auto;
}

.nav-dropdown-toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid transparent;
    background: transparent;
    cursor: pointer;
    font-family: inherit;
    box-shadow: none;
    transform: none;
}

.nav-settings-button {
    width: 38px;
    min-width: 38px;
    min-height: 34px;
    height: 34px;
    justify-content: center;
    padding: 0;
}

.nav-dropdown-toggle:hover {
    transform: none;
    box-shadow: none;
}

.nav-settings-button .nav-icon {
    width: 18px;
    height: 18px;
}

.nav-icon {
    flex: 0 0 auto;
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.nav-dropdown-panel {
    position: absolute;
    top: calc(100% + 10px);
    right: 0;
    z-index: 60;
    display: none;
    min-width: 190px;
    padding: 6px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #ffffff;
    box-shadow: 0 18px 36px rgba(14, 33, 24, 0.22);
}

.nav-dropdown.open .nav-dropdown-panel {
    display: grid;
    gap: 4px;
}

.nav-dropdown-link {
    display: flex;
    align-items: center;
    min-height: 38px;
    padding: 9px 10px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    color: var(--text);
    font-size: 0.86rem;
    font-weight: 750;
    line-height: 1.15;
    text-decoration: none;
    white-space: nowrap;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.nav-dropdown-link:hover {
    border-color: var(--line);
    background: var(--surface-soft);
    color: var(--primary-strong);
}

.nav-dropdown-link.active {
    border-color: rgba(39, 103, 73, 0.18);
    background: var(--surface-tint);
    color: var(--primary-strong);
}

.profile-chip {
    flex: 0 0 auto;
}

.menu-count-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    margin-left: 6px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--accent);
    color: #ffffff;
    font-size: 0.72rem;
    font-weight: 800;
    line-height: 1;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 0 0 auto;
    margin-left: auto;
}

.header-settings {
    order: 2;
}

.header-right .profile-chip {
    order: 1;
}

.header-right .logout-form {
    order: 3;
}

.logout-form {
    margin: 0;
}

/* Main layout */
.app-main {
    width: min(1240px, calc(100% - 32px));
    margin: 18px auto 42px;
}

.app-main-public {
    min-height: calc(100vh - 60px);
    display: grid;
    place-items: center;
}

/* Typography */
h1,
h2,
h3 {
    margin: 0 0 12px;
    color: var(--text);
    font-weight: 800;
    line-height: 1.15;
    letter-spacing: 0;
}

h1 {
    font-size: clamp(1.7rem, 2.6vw, 2.4rem);
}

h2 {
    font-size: clamp(1.22rem, 1.8vw, 1.55rem);
}

h3 {
    font-size: 1.05rem;
}

p {
    margin: 0 0 10px;
    color: var(--muted);
}

a {
    color: var(--primary);
}

/* Buttons */
a.btn,
button,
input[type="submit"] {
    appearance: none;
    min-height: 40px;
    border: 1px solid var(--primary-strong);
    border-radius: var(--radius);
    background: linear-gradient(180deg, #2f7855 0%, var(--primary) 100%);
    color: #ffffff;
    text-decoration: none;
    padding: 9px 13px;
    font-weight: 800;
    font-family: inherit;
    font-size: 0.9rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    line-height: 1.1;
    box-shadow: 0 8px 18px rgba(39, 103, 73, 0.16);
    transition: transform 0.12s ease, box-shadow 0.12s ease, background-color 0.12s ease;
}

a.btn:hover,
button:hover,
input[type="submit"]:hover {
    transform: translateY(-1px);
    box-shadow: 0 10px 22px rgba(39, 103, 73, 0.2);
}

a.btn:focus-visible,
button:focus-visible,
input[type="submit"]:focus-visible,
a:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
    outline: 3px solid rgba(193, 138, 56, 0.34);
    outline-offset: 2px;
}

a.btn.secundario,
button.secundario,
input[type="submit"].secundario,
.btn.secundario {
    background: #ffffff;
    color: var(--primary-strong);
    border-color: var(--line-strong);
    box-shadow: none;
}

a.btn.secundario:hover,
button.secundario:hover,
input[type="submit"].secundario:hover,
.btn.secundario:hover {
    background: var(--surface-tint);
    box-shadow: 0 8px 18px rgba(28, 52, 39, 0.08);
}

a.btn.danger,
button.danger,
input[type="submit"].danger {
    background: linear-gradient(180deg, #b94a40 0%, var(--danger) 100%);
    border-color: #822a24;
    color: #ffffff;
    box-shadow: 0 8px 18px rgba(163, 58, 50, 0.15);
}

/* Loading global: barra de progresso no topo */
#app-loading-bar {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0;
    z-index: 4000;
    background: linear-gradient(90deg, var(--primary), var(--accent));
    box-shadow: 0 0 10px rgba(39, 103, 73, 0.5);
    opacity: 0;
    transition: width 0.25s ease, opacity 0.35s ease;
    pointer-events: none;
}

#app-loading-bar.is-active {
    opacity: 1;
}

/* Loading global: spinner no botao ao enviar */
a.btn.is-loading,
button.is-loading,
input[type="submit"].is-loading {
    color: transparent !important;
    pointer-events: none;
    position: relative;
}

a.btn.is-loading::after,
button.is-loading::after,
input[type="submit"].is-loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-top-color: #ffffff;
    animation: app-spin 0.6s linear infinite;
}

a.btn.secundario.is-loading::after,
button.secundario.is-loading::after,
input[type="submit"].secundario.is-loading::after {
    border-color: rgba(31, 81, 58, 0.25);
    border-top-color: var(--primary-strong);
}

@keyframes app-spin {
    to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
    #app-loading-bar {
        transition: opacity 0.2s ease;
    }

    a.btn.is-loading::after,
    button.is-loading::after,
    input[type="submit"].is-loading::after {
        animation-duration: 1.2s;
    }
}

.btn-icon {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex: 0 0 16px;
}

.btn-icon.fill {
    fill: currentColor;
    stroke: none;
}

.btn-text {
    line-height: 1.15;
}

/* Forms */
form p {
    margin: 0 0 12px;
}

label {
    display: block;
    margin-bottom: 6px;
    color: #35463b;
    font-size: 0.9rem;
    font-weight: 800;
}

input,
textarea,
select {
    width: 100%;
    min-height: 40px;
    border: 1px solid var(--line-strong);
    background: #ffffff;
    border-radius: var(--radius);
    padding: 9px 11px;
    color: var(--text);
    font: inherit;
    transition: border-color 0.12s ease, box-shadow 0.12s ease, background-color 0.12s ease;
}

textarea {
    min-height: 112px;
    resize: vertical;
}

input:hover,
textarea:hover,
select:hover {
    border-color: #9bb49c;
}

input:focus,
textarea:focus,
select:focus {
    border-color: var(--primary);
    background: #ffffff;
    box-shadow: 0 0 0 4px rgba(39, 103, 73, 0.1);
    outline: none;
}

input[type="checkbox"],
input[type="radio"] {
    min-height: auto;
    accent-color: var(--primary);
}

/* Tables */
.table-wrap {
    overflow-x: auto;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow-soft);
}

table {
    width: 100%;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    border-collapse: separate;
    border-spacing: 0;
    background: #ffffff;
    box-shadow: var(--shadow-soft);
    min-width: 720px;
}

.table-wrap table {
    border: 0;
    border-radius: 0;
    box-shadow: none;
}

th,
td {
    text-align: left;
    padding: 12px 14px;
    border-bottom: 1px solid #e8eee4;
    vertical-align: top;
}

th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: #f1f6ee;
    color: #33473a;
    font-size: 0.78rem;
    font-weight: 900;
    text-transform: uppercase;
}

td {
    color: #26352d;
    font-size: 0.92rem;
}

tbody tr:last-child td,
tbody tr:last-child th {
    border-bottom: 0;
}

tbody tr:nth-child(even) td {
    background: #fbfcf8;
}

tbody tr:hover td {
    background: #f4f8f0;
}

/* Toasts */
.toast-stack {
    position: fixed;
    top: 18px;
    right: 18px;
    z-index: 80;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-width: min(92vw, 380px);
}

.toast-alert {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
    border-radius: var(--radius);
    border: 1px solid transparent;
    padding: 12px 14px;
    box-shadow: 0 12px 28px rgba(22, 37, 30, 0.18);
    background: #ffffff;
    animation: toast-in 0.2s ease;
}

.toast-content {
    color: #14251c;
    font-weight: 700;
    font-size: 0.94rem;
}

.toast-close {
    min-height: 0;
    border: 0;
    background: transparent;
    color: #4b5c51;
    font-size: 1rem;
    line-height: 1;
    padding: 2px;
    box-shadow: none;
}

.toast-close:hover {
    transform: none;
    box-shadow: none;
}

.toast-success,
.toast-info {
    background: #f0fbf3;
    border-color: #9cd4ad;
}

.toast-error,
.toast-danger {
    background: var(--danger-soft);
    border-color: #dfa09a;
}

.toast-warning {
    background: var(--accent-soft);
    border-color: #e2bf83;
}

@keyframes toast-in {
    from {
        transform: translateY(-6px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* Responsive shell */
@media (max-width: 1280px) {
    .app-header {
        align-items: stretch;
        flex-direction: column;
    }

    .header-left {
        width: 100%;
        display: grid;
        grid-template-columns: minmax(0, 1fr) 40px;
        gap: 10px;
        align-items: stretch;
    }

    .brand-wrap {
        grid-template-columns: 36px minmax(0, 1fr);
    }

    .brand-mark {
        width: 36px;
        height: 36px;
    }

    .nav-toggle {
        display: inline-flex;
        align-self: start;
        justify-self: end;
    }

    .top-nav {
        grid-column: 1 / -1;
        width: 100%;
        display: none;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }

    .top-nav.open {
        display: grid;
    }

    .top-nav .nav-link,
    .top-nav .nav-dropdown-toggle {
        width: 100%;
        justify-content: center;
        text-align: center;
        padding: 11px 10px;
    }

    .nav-settings-button {
        min-height: 40px;
        height: 40px;
        padding: 0;
    }

    .top-nav .nav-dropdown {
        width: 100%;
        grid-column: 1 / -1;
    }

    .top-nav .nav-dropdown-panel {
        position: static;
        min-width: 0;
        width: 100%;
        margin-top: 8px;
        background: rgba(255, 255, 255, 0.96);
        box-shadow: none;
    }

    .top-nav .nav-dropdown-link {
        justify-content: center;
        text-align: center;
    }

    .header-right {
        width: 100%;
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: center;
        justify-content: flex-end;
        margin-left: 0;
    }

    .header-right.has-settings {
        grid-template-columns: minmax(0, 1fr) 40px auto;
    }

    .profile-chip {
        width: 100%;
        text-align: center;
        padding: 11px 10px;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .logout-form button {
        width: auto;
        min-width: 78px;
    }

    .header-settings {
        width: auto;
    }

    .header-settings .nav-settings-button {
        width: 40px;
        min-width: 40px;
    }

    .header-settings .nav-dropdown-panel {
        position: absolute;
        top: calc(100% + 10px);
        right: 0;
        min-width: 190px;
        width: max-content;
        max-width: min(280px, calc(100vw - 24px));
        margin-top: 0;
        box-shadow: 0 18px 36px rgba(14, 33, 24, 0.22);
    }
}

@media (max-width: 865px) {
    .app-header,
    .app-main {
        width: calc(100% - 20px);
        margin-left: 10px;
        margin-right: 10px;
    }

    .app-header {
        margin-top: 10px;
        padding: 10px;
    }

    .toast-stack {
        top: 10px;
        right: 10px;
        left: 10px;
        max-width: none;
    }
}

@media (max-width: 520px) {
    h1 {
        font-size: 1.55rem;
    }

    .top-nav {
        grid-template-columns: 1fr;
    }

    /* Botoes de acao ocupam a largura toda no mobile, exceto os utilitarios
       flutuantes/inline (scroll ate o fim e envio da conversa). */
    .app-main a.btn,
    .app-main button:not(.toast-close):not(.conversation-scroll-bottom):not(.compose-send):not(.convite-excluir-btn):not(.login-password-toggle),
    .app-main input[type="submit"] {
        width: 100%;
    }
}
