/* ==========================================================================
   Polish layer: dark theme, laptop refinements, better phone ergonomics
   Loaded after app.css.
   ========================================================================== */

/* ---------- Dark theme (OS preference, or the toggle in the header) ---------- */
:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #0a111f;
    --surface: #121b2d;
    --surface-2: #17223a;
    --surface-3: #1d2a46;
    --ink: #e8edf8;
    --ink-2: #c4cee4;
    --muted: #93a0bd;
    --border: #25324f;
    --border-strong: #35446b;
    --primary: #3d6fe8;
    --primary-2: #5b88f2;
    --primary-soft: #1b2a50;
    --gold-soft: #2c2410;
    --income: #3fc789;
    --income-soft: #0f2c21;
    --expense: #ff7b6b;
    --expense-soft: #34171a;
    --warning: #f0b04a;
    --warning-soft: #2d2410;
    --info: #7aa2ff;
    --info-soft: #172647;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 8px 24px rgba(0, 0, 0, 0.35);
}
:root[data-theme="dark"] .mobile-topbar { background: rgba(10, 17, 31, 0.9); }
:root[data-theme="dark"] .bottom-nav { background: rgba(18, 27, 45, 0.96); }
:root[data-theme="dark"] .fab { box-shadow: 0 8px 20px rgba(0, 0, 0, 0.5), 0 0 0 4px var(--bg); }
:root[data-theme="dark"] .soft-button:hover,
:root[data-theme="dark"] .ghost-button:hover { background: var(--surface-3); }
:root[data-theme="dark"] .tabs .count { background: rgba(255, 255, 255, 0.1); }
:root[data-theme="dark"] input::placeholder,
:root[data-theme="dark"] textarea::placeholder { color: #6f7c99; }
:root[data-theme="dark"] .warning .stat-icon,
:root[data-theme="dark"] .sheet-option.plan .sheet-icon { color: #f0b04a; }
:root[data-theme="dark"] .choice-card:has(input[value="lent"]:checked) { color: #8fe3b6; }
:root[data-theme="dark"] .choice-card:has(input[value="borrowed"]:checked) { color: #ffb4a8; }
:root[data-theme="dark"] .mobile-brand img,
:root[data-theme="dark"] .brand img { background: #fff; }
:root[data-theme="dark"] .amounts-locked .chart-frame::after { color: var(--muted); }
:root[data-theme="dark"] dialog { background: var(--surface); color: var(--ink); }

.theme-toggle { min-width: 42px; padding-inline: 0.7rem; }

/* ---------- Laptop / desktop ---------- */
@media (min-width: 1024px) {
    .app-body .content { padding: 1.6rem clamp(1.5rem, 3vw, 3rem) 3rem; }
    .content-shell { width: min(100%, 1360px); }
    .page-header {
        position: sticky;
        top: 0;
        z-index: 20;
        margin: 0 calc(-1 * clamp(1.5rem, 3vw, 3rem)) 1.25rem;
        padding: 0.9rem clamp(1.5rem, 3vw, 3rem);
        background: color-mix(in srgb, var(--bg) 88%, transparent);
        backdrop-filter: blur(10px);
    }
    .panel, .stat-card { transition: box-shadow 0.18s ease, transform 0.18s ease, border-color 0.18s ease; }
    .stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
    tbody tr { transition: background 0.12s ease; }
    .sidebar-nav a { transition: background 0.15s ease, color 0.15s ease; }
    ::-webkit-scrollbar { width: 10px; height: 10px; }
    ::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 999px; border: 2px solid var(--bg); }
    ::-webkit-scrollbar-track { background: transparent; }
}

/* ---------- Phones & tablets ---------- */
@media (max-width: 1023px) {
    .page-header h1 { font-size: 1.45rem; }
    /* Action buttons become one swipeable row instead of wrapping into a tall block */
    .header-actions { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 0.2rem; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
    .header-actions::-webkit-scrollbar { display: none; }
    .header-actions > * { flex: 0 0 auto; }
    .header-actions form { display: inline-flex; }
    .primary-button, .ghost-button, .danger-button, .success-button, .soft-button { min-height: 44px; }
    .primary-button.small, .ghost-button.small, .danger-button.small, .success-button.small { min-height: 40px; }
    .tabs { display: flex; width: 100%; }
    .tabs a { flex: 1 0 auto; justify-content: center; min-height: 40px; }
    dialog { max-width: calc(100vw - 1.5rem); }
    .pin-dialog { width: min(92vw, 360px); }
    .bottom-nav a, .bottom-nav button:not(.fab) { min-height: 48px; justify-content: center; }
    .bottom-nav a:active, .bottom-nav button:not(.fab):active { background: var(--primary-soft); border-radius: 12px; }
}
@media (max-width: 640px) {
    .app-body .content { padding-inline: 0.85rem; }
    .mobile-topbar { margin-inline: -0.85rem; padding-inline: 0.85rem; }
    .section-header > a.primary-button { width: 100%; }
    table td .action-row { gap: 0.4rem; }
}
@media (hover: none) {
    .stat-card:hover { transform: none; }
}

kbd { padding: 1px 6px; border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 6px; font: 600 0.75rem var(--font); background: var(--surface-2); color: var(--muted); }

/* ==========================================================================
   Redesign pass: header, utilities, documents, global refinements
   ========================================================================== */

/* ----- Header utility cluster (privacy, theme, notifications) ----- */
.util-group { display: inline-flex; align-items: center; gap: 0.4rem; }
.util-group .icon-button { width: 42px; height: 42px; }
.inline-form { display: inline-flex; margin: 0; }
.lock-btn.is-locked { background: var(--gold-soft); border-color: var(--gold); color: var(--warning); }
.header-actions { gap: 0.55rem; }
.page-header { align-items: center; }
.page-title h1 { font-size: clamp(1.5rem, 2.2vw, 2rem); letter-spacing: -0.02em; }
.eyebrow { text-transform: none; }

/* ----- Dialogs (PIN, rename) ----- */
.pin-dialog { border: 0; border-radius: 20px; padding: 0; width: min(92vw, 400px); background: var(--surface); color: var(--ink); box-shadow: var(--shadow-lg); }
.pin-dialog::backdrop { background: rgba(9, 19, 43, 0.55); backdrop-filter: blur(3px); }
.pin-form { display: grid; gap: 0.8rem; padding: 1.5rem; }
.pin-form h3 { font-size: 1.25rem; }
.pin-form .muted, .muted { color: var(--muted); }
.pin-input { font-size: 2rem !important; letter-spacing: 0.9rem; text-align: center; min-height: 64px; padding-left: 1.2rem !important; }
.pin-form .action-row { display: flex; gap: 0.5rem; }
.pin-form .action-row > * { flex: 1; }

/* ----- Inline controls shouldn't stretch to full width ----- */
.action-row select, .bulk-actions select, .doc-actions select, .inline-fields select,
.action-row input[type="text"], .inline-fields input[type="text"] { width: auto; min-width: 0; }
.inline-fields { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: center; }
.inline-fields input[type="file"] { flex: 1 1 260px; }
.inline-fields input[type="text"] { flex: 1 1 180px; }

/* ----- Panels & tabs ----- */
.panel { border-radius: 18px; border: 1px solid var(--border); box-shadow: var(--shadow-sm); }
.tabs { padding: 4px; background: var(--surface-3); }
.tabs a { transition: background 0.15s ease, color 0.15s ease; }
.tabs a:hover:not(.active) { color: var(--ink); }
.tabs .count { background: color-mix(in srgb, var(--ink) 9%, transparent); }
.tabs a.active .count { background: var(--primary); color: #fff; }

.empty { display: grid; justify-items: center; gap: 0.5rem; padding: 2.5rem 1rem; text-align: center; color: var(--muted); }
.empty strong { color: var(--ink); font-size: 1.05rem; }
.empty-icon { display: grid; place-items: center; width: 56px; height: 56px; border-radius: 16px; background: var(--surface-3); color: var(--muted); }

/* ----- Documents ----- */
.docs-toolbar { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.docs-toolbar .tabs { flex: 1 1 auto; min-width: 0; }
.docs-search { flex: 0 1 280px; min-width: 200px; }

.dropzone { padding: 0; margin-bottom: 1rem; border-style: dashed; border-color: var(--border-strong); background: var(--surface-2); }
.dropzone > summary { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: 0.9rem 1.2rem; cursor: pointer; font-weight: 650; list-style: none; }
.dropzone > summary::-webkit-details-marker { display: none; }
.dropzone > summary::before { content: "＋"; margin-right: 0.6rem; color: var(--primary); font-weight: 800; }
.dropzone > summary span { flex: 1; }
.dropzone > summary small { color: var(--muted); font-weight: 500; }
.dropzone[open] > summary::before { content: "－"; }
.dropzone-body { display: grid; gap: 0.9rem; padding: 0 1.2rem 1.2rem; }

.docs-panel { overflow: hidden; }
.bulk-bar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: 0.7rem 1.1rem; border-bottom: 1px solid var(--border); background: var(--surface-2); transition: background 0.15s ease; }
.bulk-bar.has-selection { background: var(--primary-soft); }
.bulk-bar .check { display: inline-flex; align-items: center; gap: 0.6rem; font-weight: 600; cursor: pointer; }
.bulk-actions { display: flex; align-items: center; gap: 0.5rem; }
.doc-table { width: 100%; border-collapse: collapse; }
.doc-table th { padding: 0.7rem 1rem; text-align: left; font-size: 0.72rem; letter-spacing: 0.07em; text-transform: uppercase; color: var(--muted); font-weight: 700; border-bottom: 1px solid var(--border); }
.doc-table td { padding: 0.85rem 1rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.doc-table tbody tr:last-child td { border-bottom: 0; }
.doc-table tbody tr:hover td { background: var(--surface-2); }
.doc-table .col-check { width: 44px; padding-right: 0; }
.doc-table .col-actions { text-align: right; white-space: nowrap; }
.doc-name { display: flex; align-items: center; gap: 0.8rem; min-width: 0; }
.doc-name strong { display: block; line-height: 1.3; }
.file-icon { flex: 0 0 auto; display: grid; place-items: center; width: 40px; height: 44px; border-radius: 10px; font-size: 0.68rem; font-weight: 800; letter-spacing: 0.04em; background: var(--info-soft); color: var(--info); }
.file-icon[data-ext=".pdf"] { background: var(--expense-soft); color: var(--expense); }
.doc-actions { display: inline-flex; align-items: center; gap: 0.4rem; justify-content: flex-end; flex-wrap: wrap; }
.doc-actions select { min-height: 36px; padding: 0.3rem 1.8rem 0.3rem 0.6rem; font-size: 0.85rem; }

/* ----- Phones ----- */
@media (max-width: 1023px) {
    .mobile-topbar .util-group { gap: 0.3rem; }
    .mobile-topbar .icon-button { width: 40px; height: 40px; }
    .mobile-brand { flex: 1; justify-content: center; }
    .docs-search { flex: 1 1 100%; }
}
@media (max-width: 480px) {
    .mobile-brand span { display: none; }
    .mobile-brand { justify-content: flex-start; }
}
@media (max-width: 640px) {
    .bulk-bar { flex-wrap: wrap; position: sticky; top: 56px; z-index: 5; }
    .bulk-actions { width: 100%; }
    .bulk-actions select { flex: 1; }
    .doc-table tbody tr { padding: 0.9rem; }
    .doc-table .col-check { display: none; }
    .doc-table td { padding: 0.45rem 0; border-bottom: 0; text-align: left; justify-content: space-between; }
    .doc-table td[data-label=""]::before { content: none; }
    .doc-table td.col-actions { display: block; }
    .doc-actions { justify-content: flex-start; margin-top: 0.4rem; width: 100%; }
    .doc-actions .primary-button { flex: 1; }
    .dropzone > summary small { display: none; }
}

/* ----- Fixes after visual review ----- */
@media (max-width: 1023px) {
    .util-group.desktop-only { display: none; }
    .page-header { align-items: flex-start; }
    .page-header .eyebrow { margin-bottom: 0.1rem; }
    .header-actions { width: 100%; }
}
@media (min-width: 1024px) {
    .page-header { width: auto; max-width: none; }
    .page-header > * { max-width: none; }
    .docs-toolbar .tabs { flex-wrap: wrap; overflow: visible; }
}
.docs-search input { padding-left: 0.85rem; }
.bulk-bar { position: static; }
.stack:not(:has(.panel)) { display: none; }
@media (min-width: 1181px) {
    /* flex lets the left column take the full width when the right one has nothing to show */
    .grid-main-side:has(> .stack) { display: flex; gap: 1.1rem; align-items: flex-start; }
    .grid-main-side > .stack { min-width: 0; }
    .grid-main-side > .stack:first-child { flex: 1.65 1 0; }
    .grid-main-side > .stack:last-child { flex: 1 1 0; }
}
