/* Clamp — estilos da aplicação (layout, componentes, tabela, painel). */
:root {
    --brand: #2f7a94;
    --brand-strong: #235f74;
    --brand-soft: #e6f1f5;
    --ink: #1c2730;
    --ink-2: #44535e;
    --muted: #6b7a85;
    --line: #dde3e8;
    --line-strong: #c8d1d8;
    --surface: #f4f6f8;
    --card: #ffffff;
    --nav: #15303a;
    --nav-ink: #c9d6dc;
    --nav-active: #24505f;
    --ok: #1f7a4d;
    --ok-soft: #e3f3ea;
    --warn: #8a5a00;
    --warn-soft: #fdf1d8;
    --bad: #b3261e;
    --bad-soft: #fbe7e5;
    --info: #1f5f9e;
    --info-soft: #e5eef8;
    --neutral-soft: #eceff2;
    --radius: 8px;
    --shadow: 0 1px 2px rgba(16, 32, 40, .06), 0 2px 8px rgba(16, 32, 40, .06);
    --sidebar-w: 220px;
    --sidebar-w-collapsed: 64px;
    font-size: 14px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    color: var(--ink);
    background: var(--surface);
    font-size: 14px;
    line-height: 1.45;
}

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

/* ---------- Estrutura ---------- */
.app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    min-height: 100vh;
    transition: grid-template-columns .15s ease;
}
.app.is-collapsed { grid-template-columns: var(--sidebar-w-collapsed) 1fr; }
.app.is-guest { grid-template-columns: 1fr; }

.sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    background: var(--nav);
    color: var(--nav-ink);
    display: flex;
    flex-direction: column;
    padding: 14px 10px;
    gap: 4px;
    overflow: hidden;
}
.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 4px 8px 14px;
    border-bottom: 1px solid rgba(255, 255, 255, .08);
    margin-bottom: 8px;
    color: #fff;
    text-decoration: none;
    white-space: nowrap;
}
.sidebar-brand img { width: 32px; height: 32px; object-fit: contain; flex: none; }
.sidebar-brand strong { font-size: 16px; letter-spacing: .2px; }
.sidebar-brand small { display: block; font-size: 11px; color: var(--nav-ink); font-weight: 400; }

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 9px 12px;
    border-radius: 6px;
    color: var(--nav-ink);
    text-decoration: none;
    white-space: nowrap;
    font-weight: 500;
}
.nav-item i { width: 18px; text-align: center; flex: none; font-size: 15px; }
.nav-item:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.nav-item.is-active { background: var(--nav-active); color: #fff; }
.nav-spacer { flex: 1; }
.nav-user {
    padding: 10px 12px;
    font-size: 12px;
    color: var(--nav-ink);
    border-top: 1px solid rgba(255, 255, 255, .08);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.nav-toggle {
    background: none;
    border: 0;
    color: var(--nav-ink);
    text-align: left;
    cursor: pointer;
    font: inherit;
}
.app.is-collapsed .nav-label,
.app.is-collapsed .sidebar-brand span,
.app.is-collapsed .nav-user span { display: none; }
.app.is-collapsed .nav-item { justify-content: center; padding: 10px 0; }
.app.is-collapsed .sidebar-brand { justify-content: center; padding-left: 0; padding-right: 0; }

.main { min-width: 0; padding: 20px 24px 32px; }

.page-header {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 14px;
    flex-wrap: wrap;
}
.page-header h1 { font-size: 22px; font-weight: 600; margin: 0; }
.page-header .subtitle { color: var(--muted); margin: 2px 0 0; }
.page-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

/* ---------- Botões e campos ---------- */
.btn-app {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    height: 34px;
    padding: 0 13px;
    border-radius: 6px;
    border: 1px solid var(--line-strong);
    background: #fff;
    color: var(--ink);
    font: inherit;
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
}
.btn-app:hover { background: var(--surface); color: var(--ink); }
.btn-app:disabled { opacity: .5; cursor: not-allowed; }
.btn-app.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-app.primary:hover { background: var(--brand-strong); }
.btn-app.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn-app.ghost { border-color: transparent; background: transparent; }
.btn-app.icon { width: 30px; height: 30px; padding: 0; justify-content: center; }
.btn-app:focus-visible, .field:focus-visible, .field:focus { outline: 2px solid var(--brand); outline-offset: 1px; }

.field {
    height: 34px;
    border: 1px solid var(--line-strong);
    border-radius: 6px;
    padding: 0 10px;
    font: inherit;
    color: var(--ink);
    background: #fff;
    min-width: 0;
}
textarea.field { height: auto; min-height: 90px; padding: 8px 10px; resize: vertical; }
.field-group { display: flex; flex-direction: column; gap: 4px; }
.field-group label { font-size: 12px; font-weight: 600; color: var(--ink-2); }
.field-hint { font-size: 12px; color: var(--muted); }
.field-errors { color: var(--bad); font-size: 12px; margin: 0; padding-left: 16px; }

/* ---------- Barra de ferramentas da tabela ---------- */
.toolbar {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    padding: 12px 14px;
    flex-wrap: wrap;
    border-bottom: 1px solid var(--line);
}
.toolbar .grow { flex: 1; }
.toolbar .field-group .field { width: 150px; }
.filter-count {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--brand-strong);
    background: var(--brand-soft);
    border-radius: 999px;
    padding: 3px 10px;
    font-size: 12px;
    font-weight: 600;
}

.dropdown-app { position: relative; }
.dropdown-panel {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    z-index: 30;
    width: 280px;
    max-height: 420px;
    overflow: auto;
    padding: 8px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: 0 8px 24px rgba(16, 32, 40, .16);
}
.dropdown-panel[hidden] { display: none; }
.dropdown-panel label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 5px 6px;
    border-radius: 4px;
    cursor: pointer;
}
.dropdown-panel label:hover { background: var(--surface); }
.dropdown-panel .panel-footer {
    display: flex;
    justify-content: space-between;
    border-top: 1px solid var(--line);
    margin-top: 6px;
    padding-top: 8px;
}

/* ---------- Tabela ---------- */
.table-card { overflow: hidden; }
.table-scroll {
    overflow: auto;
    max-height: calc(100vh - 250px);
    min-height: 320px;
    position: relative;
}
table.grid {
    border-collapse: separate;
    border-spacing: 0;
    width: max-content;
    min-width: 100%;
    font-size: 13px;
}
.grid th, .grid td {
    padding: 7px 10px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: middle;
    background: #fff;
}
.grid thead th {
    position: sticky;
    z-index: 3;
    background: #f8fafb;
    font-weight: 600;
    color: var(--ink-2);
    white-space: nowrap;
}
.grid thead tr.head-titles th { top: 0; border-bottom: 1px solid var(--line); }
.grid thead tr.head-filters th { top: var(--head-h, 36px); padding: 6px 6px 8px; border-bottom: 2px solid var(--line-strong); }
.grid .sticky-col { position: sticky; z-index: 2; }
.grid thead .sticky-col { z-index: 4; }
.grid .sticky-col.is-last-sticky { box-shadow: 2px 0 0 var(--line); }
.grid tbody tr:nth-child(even) td { background: #fbfcfd; }
.grid tbody tr:hover td { background: var(--brand-soft); }
.grid td.cell-text {
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.grid td.cell-long { max-width: 320px; }
.grid td .empty { color: var(--line-strong); }
.grid td.cell-actions { white-space: nowrap; width: 76px; }
.grid td.cell-actions .btn-app { height: 28px; width: 28px; }

.sort-btn {
    all: unset;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}
.sort-btn i { color: var(--line-strong); font-size: 11px; }
.sort-btn.is-sorted i { color: var(--brand); }
.sort-btn:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; border-radius: 3px; }

.head-filters .field { height: 28px; font-size: 12px; padding: 0 7px; width: 100%; min-width: 72px; font-weight: 400; }

/* Situação: letra sempre visível + cor de apoio */
.badge-status {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 26px;
    height: 22px;
    padding: 0 7px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    background: var(--neutral-soft);
    color: var(--ink-2);
}
.badge-status.s-S { background: var(--ok-soft); color: var(--ok); }
.badge-status.s-N { background: var(--warn-soft); color: var(--warn); }
.badge-status.s-P { background: var(--info-soft); color: var(--info); }
.badge-status.s-X { background: var(--bad-soft); color: var(--bad); }

.table-state {
    padding: 48px 16px;
    text-align: center;
    color: var(--muted);
}
.table-loading {
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, .6);
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding-top: 90px;
    z-index: 5;
    font-weight: 500;
    color: var(--ink-2);
}
.table-loading[hidden] { display: none; }

.pager {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    border-top: 1px solid var(--line);
    flex-wrap: wrap;
}
.pager-info { color: var(--muted); }
.pager-controls { display: flex; align-items: center; gap: 6px; }
.pager-controls .field { width: 64px; text-align: center; }

.legend-inline { display: flex; gap: 10px; align-items: center; color: var(--muted); font-size: 12px; flex-wrap: wrap; }

/* ---------- Modais ---------- */
.modal-backdrop-app {
    position: fixed;
    inset: 0;
    background: rgba(16, 28, 35, .45);
    z-index: 50;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 40px 16px;
    overflow: auto;
}
.modal-app {
    background: #fff;
    border-radius: 10px;
    width: min(980px, 100%);
    box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
.modal-app.small { width: min(440px, 100%); }
.modal-app header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--line);
}
.modal-app header h2 { font-size: 18px; margin: 0; font-weight: 600; }
.modal-app header p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.modal-app .modal-body { padding: 16px 20px; }
.modal-app footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 14px 20px;
    border-top: 1px solid var(--line);
    background: #fafbfc;
    border-radius: 0 0 10px 10px;
}

.form-section { margin-bottom: 18px; }
.form-section h3 {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .5px;
    color: var(--muted);
    margin: 0 0 10px;
    font-weight: 600;
}
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 12px 14px;
}
.form-grid .span-2 { grid-column: span 2; }
.form-grid .span-all { grid-column: 1 / -1; }

.codes-box { border: 1px solid var(--line); border-radius: 6px; padding: 10px; }
.codes-list { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.codes-list li {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 4px 6px 4px 10px;
    font-variant-numeric: tabular-nums;
}
.codes-list select { height: 26px; border: 1px solid var(--line-strong); border-radius: 4px; font: inherit; font-size: 12px; }
.codes-add { display: flex; gap: 6px; }
.codes-add .field { width: 160px; }

/* ---------- Mensagens (toasts) ---------- */
.message {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 80;
    max-width: 420px;
    padding: 12px 16px;
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
    background: #fff;
    border-left: 4px solid var(--brand);
    animation: toast-in .18s ease-out;
}
.message.sucess { border-left-color: var(--ok); }
.message.error { border-left-color: var(--bad); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

.flash { margin: 0 0 14px; padding: 10px 14px; border-radius: 6px; background: var(--warn-soft); color: var(--warn); }
.flash.error { background: var(--bad-soft); color: var(--bad); }
.flash.success { background: var(--ok-soft); color: var(--ok); }
.flash ul { margin: 0; padding-left: 18px; }

/* ---------- Login ---------- */
.login-page {
    min-height: 100vh;
    display: grid;
    place-items: center;
    background: linear-gradient(160deg, var(--nav) 0%, #1f4b5a 55%, var(--brand) 100%);
    padding: 24px;
}
.login-card { width: min(380px, 100%); padding: 28px; }
.login-card .brand { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.login-card .brand img { width: 44px; height: 44px; }
.login-card h1 { font-size: 20px; margin: 0; }
.login-card .brand p { margin: 0; color: var(--muted); font-size: 13px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; }
.login-card .field { width: 100%; height: 40px; }
.login-card .btn-app { height: 40px; justify-content: center; }

/* ---------- Painel ---------- */
.filters-row {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    padding: 12px 14px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 16px; }
.kpi { padding: 14px 16px; }
.kpi .kpi-label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.kpi .kpi-value { font-size: 28px; font-weight: 600; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .kpi-note { color: var(--muted); font-size: 12px; }
.kpi a { text-decoration: none; }

.panels { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 16px; }
.panel { padding: 14px 16px 10px; min-width: 0; }
.panel.wide { grid-column: 1 / -1; }
.panel h2 { font-size: 15px; font-weight: 600; margin: 0 0 2px; }
.panel .panel-sub { color: var(--muted); font-size: 12px; margin: 0 0 12px; }

.bars { list-style: none; margin: 0; padding: 0; }
.bars li { margin-bottom: 4px; }
.bar-row {
    display: grid;
    grid-template-columns: minmax(110px, 38%) 1fr 64px;
    align-items: center;
    gap: 10px;
    padding: 4px 6px;
    border-radius: 6px;
    color: var(--ink);
    text-decoration: none;
}
a.bar-row:hover { background: var(--surface); }
.bar-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.bar-track { height: 12px; background: transparent; }
.bar-fill { height: 12px; background: var(--brand); border-radius: 0 4px 4px 0; min-width: 2px; }
.bar-value { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink-2); font-size: 13px; }

.status-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; }
.status-grid h3 { font-size: 13px; margin: 0 0 6px; font-weight: 600; }

.columns-chart { position: relative; }
.columns-plot {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    height: 220px;
    border-bottom: 1px solid var(--line-strong);
    padding-top: 22px;
}
.col-item { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 0; position: relative; }
.col-bar { width: 70%; max-width: 38px; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 2px; }
.col-item:hover .col-bar { background: var(--brand-strong); }
.col-item .col-tip {
    position: absolute;
    bottom: calc(100% + 4px);
    background: var(--ink);
    color: #fff;
    font-size: 12px;
    padding: 4px 8px;
    border-radius: 4px;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transform: translateY(var(--tip-y, 0));
}
.col-item:hover .col-tip { opacity: 1; }
.columns-axis { display: flex; gap: 4px; margin-top: 6px; }
.columns-axis span { flex: 1; text-align: center; font-size: 11px; color: var(--muted); min-width: 0; overflow: hidden; }

.data-table-toggle { margin-top: 8px; }
.data-table-toggle summary { cursor: pointer; color: var(--brand); font-size: 12px; }
.data-table-toggle table { margin-top: 8px; border-collapse: collapse; font-size: 12px; }
.data-table-toggle td, .data-table-toggle th { padding: 3px 10px 3px 0; border-bottom: 1px solid var(--line); text-align: left; }
.data-table-toggle td:last-child { text-align: right; font-variant-numeric: tabular-nums; }

.muted { color: var(--muted); }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* Widgets gerados pelo Django (forms.py usa "form-control") */
.form-control { height: 34px; border: 1px solid var(--line-strong); border-radius: 6px; padding: 0 10px; font: inherit; width: 100%; }
textarea.form-control { height: auto; min-height: 90px; padding: 8px 10px; }

/* Ajustes */
[hidden] { display: none !important; }
a.kpi, a.kpi:hover { text-decoration: none; color: inherit; }
a.kpi:hover { border-color: var(--brand); }
.panels { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid td, .grid th { padding-top: 6px; padding-bottom: 6px; }
.grid td.cell-actions .btn-app { height: 26px; width: 26px; }
.filters-row [data-range] { height: 30px; border-color: var(--line); background: #fff; }
.modal-app { display: flex; flex-direction: column; max-height: calc(100vh - 80px); }
.modal-app .modal-body { overflow: auto; }
.modal-backdrop-app { align-items: center; }
