/* =====================================================================
   Design tokens
   Brand: Dichotome — slate blue-gray + chartreuse, sampled from the
   client logo. Layout language borrowed from a fintech-dashboard
   reference the client shared: soft rounded cards on a pale gray
   canvas, colorful icon chips, a pill-shaped active nav state, and a
   top bar with search + avatar. Chip colors stay inside the brand
   palette (slate / paid-green / rust / lime) rather than the
   reference's blue/green/orange/purple.
   ===================================================================== */
:root {
    --color-bg:        #F1F4F5;
    --color-surface:   #FFFFFF;
    --color-ink:       #2A3B42;
    --color-muted:     #6C8894;
    --color-border:    #E7ECED;
    --color-sidebar:   #FFFFFF;
    --color-sidebar-ink: #8C9CA3;
    --color-sidebar-active: #EEF7C9;
    --color-accent:    #435F6E;
    --color-accent-hover: #324750;
    --color-lime:      #D2F03C;
    --color-lime-ink:  #5C6B12;
    --color-paid:      #2F7A5C;
    --color-overdue:   #C0533A;
    --color-draft:     #8B96A0;
    --color-sent:      #7D97A3;

    --chip-slate-bg:   #E7EEF0;
    --chip-paid-bg:    #E1F0E8;
    --chip-overdue-bg: #F8E7E1;
    --chip-lime-bg:    #F3FAD2;

    --font-display: 'interstate', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-body: 'interstate', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'IBM Plex Mono', 'SFMono-Regular', Consolas, monospace;

    --radius: 10px;
    --radius-lg: 16px;
    --radius-pill: 100px;
    --shadow-card: 0 1px 2px rgba(42, 59, 66, 0.04), 0 8px 24px rgba(42, 59, 66, 0.06);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--color-bg);
    color: var(--color-ink);
    font-family: var(--font-body);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

h1, h2, h3 {
    font-family: var(--font-display);
    font-weight: 700;
    letter-spacing: -0.01em;
    margin: 0 0 0.5em;
}

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

.figure, .amount, td.amount, .tabular {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
}

svg.icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

/* ---------------------------------------------------------------------
   App shell: sidebar + main, with a top bar spanning the content column
   --------------------------------------------------------------------- */
.app-shell {
    display: grid;
    grid-template-columns: 232px 1fr;
    min-height: 100vh;
}

/* The content column (topbar + main) is a grid item, which defaults to
   min-width:auto — meaning it won't shrink below its widest descendant
   (e.g. a min-width table). Without this, a wide table pushes the whole
   page wider instead of scrolling within .table-scroll. */
.app-shell > div {
    min-width: 0;
}

.sidebar {
    background: var(--color-sidebar);
    border-right: 1px solid var(--color-border);
    color: var(--color-sidebar-ink);
    padding: 24px 16px;
    display: flex;
    flex-direction: column;
}

.sidebar .brand-logo {
    display: block;
    height: 26px;
    width: auto;
    align-self: flex-start;
    margin: 0 8px 32px;
}

.sidebar nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.sidebar nav a {
    color: var(--color-sidebar-ink);
    text-decoration: none;
    padding: 10px 14px;
    border-radius: var(--radius-pill);
    font-size: 0.92rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 12px;
}

.sidebar nav a svg { stroke: var(--color-sidebar-ink); }
.sidebar nav a:hover { background: #F6F8F8; color: var(--color-ink); }
.sidebar nav a:hover svg { stroke: var(--color-ink); }
.sidebar nav a.active { background: var(--color-lime); color: var(--color-lime-ink); }
.sidebar nav a.active svg { stroke: var(--color-lime-ink); }

.sidebar .sidebar-footer {
    margin-top: auto;
    padding-top: 16px;
    border-top: 1px solid var(--color-border);
    font-size: 0.85rem;
    color: var(--color-muted);
}

.sidebar .sidebar-footer a {
    color: var(--color-sidebar-ink);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 14px;
    text-decoration: none;
    border-radius: var(--radius-pill);
}
.sidebar .sidebar-footer a svg { stroke: var(--color-sidebar-ink); }
.sidebar .sidebar-footer a:hover { background: #F6F8F8; color: var(--color-ink); }
.sidebar .sidebar-footer a:hover svg { stroke: var(--color-ink); }
.sidebar .sidebar-footer a.active { background: var(--color-lime); color: var(--color-lime-ink); }
.sidebar .sidebar-footer a.active svg { stroke: var(--color-lime-ink); }
.sidebar .sidebar-footer form { margin: 0; }
.sidebar .sidebar-footer button.link-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    background: none;
    border: none;
    color: var(--color-sidebar-ink);
    padding: 8px 14px;
    border-radius: var(--radius-pill);
    cursor: pointer;
    font-size: 0.85rem;
    font-family: inherit;
    text-align: left;
}
.sidebar .sidebar-footer button.link-btn svg { stroke: var(--color-sidebar-ink); }
.sidebar .sidebar-footer button.link-btn:hover { background: #F6F8F8; color: var(--color-ink); }
.sidebar .sidebar-footer button.link-btn:hover svg { stroke: var(--color-ink); }

/* ---------------------------------------------------------------------
   Top bar
   --------------------------------------------------------------------- */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    padding: 20px 32px;
    background: var(--color-bg);
}

.topbar h1 {
    font-size: 1.5rem;
    margin: 0;
    white-space: nowrap;
}

.topbar-search {
    flex: 1;
    max-width: 420px;
    position: relative;
}

.topbar-search svg {
    position: absolute;
    left: 14px;
    top: 50%;
    transform: translateY(-50%);
    stroke: var(--color-muted);
    width: 16px;
    height: 16px;
}

.topbar-search input[type=text] {
    width: 100%;
    padding: 10px 14px 10px 42px;
    border-radius: var(--radius-pill);
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    font-size: 0.88rem;
    font-family: inherit;
}

.topbar-right {
    display: flex;
    align-items: center;
    gap: 18px;
}

.topbar-new-invoice {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 38px;
    padding: 0 16px;
    border-radius: var(--radius-pill);
    background: #FAFBFB;
    border: 1px solid #2A3B42;
    color: #000;
    font-weight: 700;
    font-size: 0.85rem;
    text-decoration: none;
    white-space: nowrap;
}
.topbar-new-invoice svg { stroke: #000; }
.topbar-new-invoice:hover { background: #EFF1F1; }

.topbar-bell {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.topbar-bell svg { stroke: var(--color-muted); width: 18px; height: 18px; }

.topbar-user {
    display: flex;
    align-items: center;
    gap: 10px;
}

.topbar-avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--color-accent);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.85rem;
    flex-shrink: 0;
}

.topbar-user .name {
    font-size: 0.85rem;
    font-weight: 700;
    line-height: 1.2;
    white-space: nowrap;
}

.topbar-user .role {
    font-size: 0.75rem;
    color: var(--color-muted);
    line-height: 1.2;
}

main.content {
    padding: 0 32px 32px;
}

/* ---------------------------------------------------------------------
   Cards
   --------------------------------------------------------------------- */
.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    padding: 20px 22px;
}

.card + .card { margin-top: 16px; }
.dashboard-grid > .card + .card { margin-top: 0; }

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.card-header h2 {
    font-size: 1rem;
    margin: 0;
}

.dashboard-grid {
    display: grid;
    grid-template-columns: 1fr 1.3fr;
    align-items: stretch;
    gap: 16px;
    margin-bottom: 16px;
}

.dashboard-grid > .card,
.dashboard-grid > .stat-grid-2x2 {
    height: 100%;
    min-width: 0;
}

.stat-grid-2x2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
}

/* Stat cards: colored icon chip, kebab menu, big figure, delta pill */
.stat-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
    padding: 18px 20px;
}

.stat-card-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 14px;
}

.stat-chip {
    width: 38px;
    height: 38px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
}
.stat-chip.chip-slate   { background: var(--chip-slate-bg); }
.stat-chip.chip-paid    { background: var(--chip-paid-bg); }
.stat-chip.chip-overdue { background: var(--chip-overdue-bg); }
.stat-chip.chip-lime    { background: var(--chip-lime-bg); }

.stat-chip.chip-slate svg   { stroke: var(--color-accent); }
.stat-chip.chip-paid svg    { stroke: var(--color-paid); }
.stat-chip.chip-overdue svg { stroke: var(--color-overdue); }
.stat-chip.chip-lime svg    { stroke: var(--color-lime-ink); }

.stat-kebab { color: var(--color-muted); font-size: 1.1rem; line-height: 1; padding: 2px 4px; }

.stat-card .label {
    color: var(--color-muted);
    font-size: 0.82rem;
    margin-bottom: 2px;
}

.stat-card .value-row {
    display: flex;
    align-items: baseline;
    gap: 10px;
    flex-wrap: wrap;
}

.stat-card .value {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    font-size: 1.4rem;
    font-weight: 500;
}

.delta {
    font-size: 0.78rem;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    gap: 2px;
}
.delta.up { color: var(--color-paid); }
.delta.down { color: var(--color-overdue); }

/* ---------------------------------------------------------------------
   Chart card
   --------------------------------------------------------------------- */
.chart-card canvas {
    max-height: 220px;
}

.select-pill {
    font-size: 0.8rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-pill);
    padding: 5px 12px;
    background: var(--color-surface);
    color: var(--color-ink);
    font-family: inherit;
}

/* ---------------------------------------------------------------------
   Tables
   --------------------------------------------------------------------- */
table {
    width: 100%;
    border-collapse: collapse;
}

thead th {
    text-align: left;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--color-muted);
    font-weight: 700;
    padding: 13px 22px;
    border-bottom: 1px solid var(--color-border);
}

tbody td {
    padding: 18px 22px;
    border-bottom: 1px solid var(--color-border);
    font-size: 0.9rem;
}

tbody tr:last-child td { border-bottom: none; }

/* Small key-value / totals tables that sit inside an already-padded
   .card (rather than a zero-padding card + separate header bar) —
   they don't need the 22px horizontal inset a second time. */
.table-plain thead th,
.table-plain tbody td {
    padding: 18px 6px;
}
.table-plain thead th:first-child,
.table-plain tbody td:first-child {
    padding-left: 0;
}
.table-plain thead th:last-child,
.table-plain tbody td:last-child {
    padding-right: 0;
}
tbody tr:hover { background: #FAFBFB; }

td.amount, th.amount { text-align: right; }

.status-dot-row {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.85rem;
    font-weight: 700;
}

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    display: inline-block;
}
.status-dot.status-paid     { background: var(--color-paid); }
.status-dot.status-overdue  { background: var(--color-overdue); }
.status-dot.status-draft    { background: var(--color-draft); }
.status-dot.status-sent,
.status-dot.status-viewed   { background: var(--color-sent); }
.status-dot.status-partial  { background: #C9960E; }
.status-dot.status-accepted { background: var(--color-paid); }
.status-dot.status-declined { background: var(--color-overdue); }
.status-dot.status-expired  { background: var(--color-draft); }
.status-dot.status-converted { background: var(--color-lime-ink); }
.status-dot.status-void { background: var(--color-draft); }

.status-dot-row.status-paid     { color: var(--color-paid); }
.status-dot-row.status-overdue  { color: var(--color-overdue); }
.status-dot-row.status-draft    { color: var(--color-draft); }
.status-dot-row.status-sent,
.status-dot-row.status-viewed   { color: var(--color-sent); }
.status-dot-row.status-partial  { color: #96700B; }
.status-dot-row.status-accepted { color: var(--color-paid); }
.status-dot-row.status-declined { color: var(--color-overdue); }
.status-dot-row.status-expired  { color: var(--color-draft); }
.status-dot-row.status-converted { color: var(--color-lime-ink); }
.status-dot-row.status-void { color: var(--color-draft); }

/* ---------------------------------------------------------------------
   Forms & buttons
   --------------------------------------------------------------------- */
label {
    display: block;
    font-size: 0.85rem;
    font-weight: 700;
    margin-bottom: 6px;
}

input[type=text], input[type=email], input[type=password],
input[type=number], input[type=date], select, textarea {
    width: 100%;
    padding: 9px 11px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    font-family: var(--font-body);
    font-size: 0.92rem;
    background: #fff;
    color: var(--color-ink);
}

input:focus, select:focus, textarea:focus {
    outline: 2px solid var(--color-accent);
    outline-offset: 1px;
}

.field { margin-bottom: 16px; }

.btn {
    display: inline-block;
    padding: 10px 18px;
    border-radius: var(--radius-pill);
    font-size: 0.9rem;
    font-weight: 700;
    font-family: inherit;
    line-height: 1.2;
    text-decoration: none;
    border: 1px solid transparent;
    cursor: pointer;
    margin: 0;
    vertical-align: middle;
    appearance: none;
    -webkit-appearance: none;
}

.btn-primary { background: var(--color-accent); color: #fff; }
.btn-primary:hover { background: var(--color-accent-hover); }

.btn-secondary { background: #fff; color: var(--color-ink); border-color: var(--color-border); }
.btn-secondary:hover { background: #F5F4F0; }

.alert {
    padding: 12px 14px;
    border-radius: var(--radius);
    font-size: 0.88rem;
    margin-bottom: 16px;
}
.alert-error { background: #FBEAE5; color: #8A3A22; border: 1px solid #F0C6B7; }
.alert-success { background: #E4F2EA; color: #1F5B40; border: 1px solid #BFE0CD; }

/* ---------------------------------------------------------------------
   Login page (no sidebar)
   --------------------------------------------------------------------- */
.auth-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-accent);
}

.auth-card {
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    padding: 40px 36px;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 20px 50px rgba(0,0,0,0.25);
}

.auth-card .brand-logo {
    height: 32px;
    width: auto;
    margin-bottom: 18px;
}

.auth-card p.tagline {
    color: var(--color-muted);
    margin-top: 0;
    margin-bottom: 24px;
    font-size: 0.9rem;
}

.btn-block { width: 100%; text-align: center; }

/* ---------------------------------------------------------------------
   Responsive form grids (replace ad-hoc inline grid-template-columns
   so a media query can actually override them — inline styles can't
   be beaten by a plain CSS rule regardless of specificity).
   --------------------------------------------------------------------- */
.form-grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }
.settings-item-grid { display: grid; grid-template-columns: 2fr 1fr 1fr auto auto; gap: 12px; align-items: end; }

/* Two named variants of the show-page two-column layout, since the
   column ratio differs per page but both need the same responsive fix. */
.dashboard-grid--client { grid-template-columns: 1fr 1.4fr; }
.dashboard-grid--doc { grid-template-columns: 1.6fr 1fr; }

/* Fixed-width totals/summary boxes — width is intentional on desktop
   (they're meant to look like a receipt strip, not stretch full width)
   but need to become fluid on narrow screens. */
.totals-box { width: 320px; margin-left: auto; }
.totals-box--sm { width: 280px; margin-left: auto; }

/* Lets any data table scroll horizontally on narrow screens instead of
   squeezing columns unreadably or breaking the page layout. */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Small red delete-icon button used at the end of list-table rows
   (estimates, invoices) — deliberately minimal so it doesn't compete
   visually with the row's actual content. */
.row-delete-btn {
    background: none;
    border: none;
    color: var(--color-overdue);
    cursor: pointer;
    font-size: 1.05rem;
    line-height: 1;
    padding: 4px 6px;
    border-radius: 6px;
    opacity: 0.7;
    transition: opacity 0.15s, background 0.15s;
}
.row-delete-btn:hover {
    opacity: 1;
    background: var(--chip-overdue-bg);
}

/* Line-item tables (editable in forms, read-only on detail pages) have
   enough columns that they need a floor width — otherwise the table
   just shrinks its cells into unusable slivers instead of scrolling. */
#items-table,
.line-items-view {
    min-width: 620px;
}

/* ---------------------------------------------------------------------
   Mobile nav: hamburger button + off-canvas sidebar drawer
   --------------------------------------------------------------------- */
.mobile-menu-btn {
    display: none;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    flex-shrink: 0;
    cursor: pointer;
}
.mobile-menu-btn svg { stroke: var(--color-ink); }

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(20, 28, 32, 0.4);
    z-index: 150;
}
.sidebar-backdrop.open { display: block; }

@media (max-width: 980px) {
    .dashboard-grid,
    .dashboard-grid--client,
    .dashboard-grid--doc {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 700px) {
    .form-grid-2,
    .form-grid-3,
    .settings-item-grid {
        grid-template-columns: 1fr;
    }
    .totals-box,
    .totals-box--sm {
        width: 100%;
    }
}

@media (max-width: 860px) {
    .app-shell { grid-template-columns: 1fr; }

    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: 240px;
        z-index: 200;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: 0 0 0 rgba(0,0,0,0);
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 8px 0 24px rgba(20, 28, 32, 0.15);
    }

    .mobile-menu-btn { display: flex; }

    .stat-grid-2x2 { grid-template-columns: 1fr; }
    .topbar { flex-wrap: wrap; padding: 16px 20px; gap: 12px; }
    .topbar h1 { font-size: 1.25rem; }
    main.content { padding: 0 20px 20px; }
}

@media (max-width: 480px) {
    .topbar-user .name, .topbar-user .role { display: none; }
}

/* ---------------------------------------------------------------------
   Client portal — lighter shell than the admin app (no sidebar), reuses
   .card / table-plain / status-dot / .btn styles from the rest of the app.
   --------------------------------------------------------------------- */
.portal-shell {
    max-width: 1160px;
    margin: 0 auto;
    padding: 0 20px 40px;
}

.portal-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 28px 0;
}

.portal-topbar img { height: 26px; width: auto; }

.portal-topbar .portal-user {
    font-size: 0.85rem;
    color: var(--color-muted);
    display: flex;
    align-items: center;
    gap: 14px;
}

.portal-auth-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-accent);
}

@media (prefers-reduced-motion: reduce) {
    * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---------------------------------------------------------------------
   Description field typeahead (catalog item suggestions)
   --------------------------------------------------------------------- */
.typeahead-dropdown {
    position: fixed;
    z-index: 1000;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-card);
    max-height: 220px;
    overflow-y: auto;
}

.typeahead-item {
    padding: 8px 12px;
    font-size: 0.85rem;
    display: flex;
    justify-content: space-between;
    gap: 10px;
    cursor: pointer;
}

.typeahead-item:hover,
.typeahead-item.active {
    background: #F4F7F7;
}

.typeahead-rate {
    color: var(--color-muted);
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}
