:root {
    --paper: #f4ecdf;
    --paper-2: #fffaf3;
    --ink: #241c16;
    --muted: #6f6256;
    --line: #e4d5c2;
    --teal: #143f3b;
    --teal-2: #1d5c55;
    --gold: #8d5a12;
    --income: #0d6b4c;
    --expense: #9a3412;
    --shadow: 0 16px 40px rgba(55, 36, 16, 0.08);
    --sidebar: #122c29;
    --chart-in: #0a9a78;
    --chart-out: #d2691e;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { scroll-behavior: smooth; }

body {
    margin: 0;
    min-height: 100vh;
    color: var(--ink);
    background:
        radial-gradient(900px 420px at 0% -10%, rgba(141, 90, 18, 0.12), transparent 55%),
        linear-gradient(180deg, #f7f1e6 0%, #efe3d0 100%);
    font-family: "IBM Plex Sans Thai", "Sukhumvit Set", "Thonburi", sans-serif;
    font-size: 16px;
    line-height: 1.5;
}

h1, h2, h3 {
    font-family: "Noto Serif Thai", "Thonburi", serif;
    font-weight: 600;
    line-height: 1.25;
}

h1 { margin: 0; font-size: 2rem; }
h2 { margin: 0 0 12px; font-size: 1.35rem; }
h3 { margin: 18px 0 8px; font-size: 1.05rem; }
p { margin: 0 0 10px; }
a { color: var(--teal-2); }
code { font-size: 0.9rem; word-break: break-word; }

.pos { color: var(--income); }
.neg { color: var(--expense); }
.muted, .field span, .stat p, th { color: var(--muted); }

.shell {
    display: grid;
    grid-template-columns: 248px minmax(0, 1fr);
    min-height: 100vh;
}

.sidebar {
    background:
        linear-gradient(180deg, rgba(200, 161, 90, 0.16), transparent 28%),
        var(--sidebar);
    color: #f6f1e7;
    padding: 22px 16px;
    display: flex;
    flex-direction: column;
    gap: 22px;
}

.brand-row, .panel-head, .page-head, .plain-list li, .row-actions, .form-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.brand {
    display: flex;
    gap: 10px;
    align-items: center;
    color: inherit;
    text-decoration: none;
}

.brand strong, .brand small { display: block; }
.brand small { color: #d9c7a4; font-size: 0.78rem; }

.brand-mark {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border-radius: 13px;
    background: #e2c48a;
    color: #1d1812;
    font-family: "Noto Serif Thai", serif;
    font-size: 1.3rem;
}

.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
    color: #e7efe9;
    text-decoration: none;
    padding: 10px 12px;
    border-radius: 12px;
}
.nav a:hover, .nav a[aria-current="page"] { background: rgba(255, 255, 255, 0.1); }
.nav a[aria-current="page"] { color: white; }

.sidebar-foot { margin-top: auto; }
.who { margin-bottom: 10px; }
.who strong, .who span { display: block; }
.who span { color: #d9c7a4; font-size: 0.85rem; }

.nav-toggle { display: none; }

.main { min-width: 0; padding: 28px 32px 56px; }

.page-head { align-items: flex-end; margin-bottom: 18px; flex-wrap: wrap; }
.eyebrow, .brand-kicker {
    margin: 0 0 4px;
    color: var(--gold);
    font-size: 0.82rem;
    font-weight: 600;
}
.head-actions, .form-actions, .row-actions { flex-wrap: wrap; justify-content: flex-start; }

.stats, .split, .cal-layout, .form-grid {
    display: grid;
    gap: 14px;
}
.stats { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 14px; }
.stats-tight { margin: 8px 0 16px; }
.stats-compact { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.split { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
.cal-layout { grid-template-columns: minmax(0, 1fr) minmax(240px, 340px); align-items: start; }
.form-grid { grid-template-columns: 1fr 1fr; }
.wide { grid-column: 1 / -1; }

.card, .stat {
    background: rgba(255, 250, 243, 0.94);
    border: 1px solid var(--line);
    border-radius: 18px;
    box-shadow: var(--shadow);
}
.card { padding: 18px; margin-bottom: 16px; }
.stat { padding: 14px 16px; }
.stat p { margin: 0; font-size: 0.86rem; }
.stat strong { display: block; margin-top: 4px; font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.narrow { max-width: 560px; }
.page-note { margin-top: -6px; }

.stat small { display: block; margin-top: 2px; color: var(--muted); font-size: 0.8rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.list-flush { margin-top: 0; }
.list-flush li:first-child { border-top: 0; padding-top: 0; }
.panel-head h2 { margin-bottom: 0; }
.panel-head { margin-bottom: 12px; flex-wrap: wrap; }

.sort-hint { font-size: 0.85rem; margin-bottom: 0; }
.sort-status { font-size: 0.85rem; }
.sortable li { justify-content: flex-start; align-items: center; background: transparent; }
.sortable .sort-name { flex: 1; min-width: 0; }
.sortable li.is-dragging { background: #f3f7f4; border-radius: 12px; box-shadow: var(--shadow); position: relative; z-index: 1; }
.drag-handle {
    flex: none;
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--muted);
    font-size: 1.2rem;
    line-height: 1;
    cursor: grab;
    touch-action: none;
}
.drag-handle:hover, .drag-handle:focus-visible { background: rgba(20, 63, 59, 0.08); color: var(--teal-2); outline: none; }
.is-dragging .drag-handle { cursor: grabbing; }
.balance-row { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin: 6px 0 4px; }
.balance-row strong { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.balance-note { font-size: 0.88rem; margin-bottom: 12px; }
.field-hint { font-size: 0.85rem; margin: -6px 0 0; }
.account { padding: 14px 0; border-top: 1px solid var(--line); }
.account:first-of-type { border-top: 0; padding-top: 0; }
.account .panel-head { align-items: flex-start; }
.account .panel-head p { margin: 0; }

.scope-tabs {
    display: inline-flex;
    gap: 4px;
    margin-bottom: 14px;
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: rgba(255, 250, 243, 0.94);
}
.scope-tabs a { padding: 6px 16px; border-radius: 999px; color: var(--ink); font-weight: 600; text-decoration: none; }
.scope-tabs a[aria-current="page"] { background: var(--teal); color: white; }
.member-filter { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 14px; }
.chip { cursor: pointer; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip span {
    display: inline-block;
    padding: 5px 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: white;
    font-size: 0.9rem;
}
.chip input:checked + span { background: var(--teal); border-color: var(--teal); color: white; }
.chip input:focus-visible + span { outline: 3px solid rgba(29, 92, 85, 0.3); }
.cell-sub { display: block; color: var(--muted); }
tfoot th { color: var(--ink); font-size: inherit; border-bottom: 0; }

.legend { display: flex; gap: 14px; list-style: none; margin: 0; padding: 0; font-size: 0.85rem; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: none; }
.swatch-in, .bar-in { background: var(--chart-in); }
.swatch-out, .bar-out { background: var(--chart-out); }

.trend { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 6px; }
.trend-col {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 6px 4px;
    border-radius: 12px;
    color: var(--muted);
    text-decoration: none;
}
.trend-col:hover, .trend-col:focus-visible { background: rgba(20, 63, 59, 0.06); outline: none; }
.trend-col.is-current { color: var(--ink); font-weight: 600; }
.trend-bars {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    gap: 2px;
    height: 180px;
    border-bottom: 1px solid var(--line);
}
.trend-bar { width: min(18px, 38%); min-height: 1px; border-radius: 4px 4px 0 0; }
.trend-label { text-align: center; font-size: 0.82rem; }
.trend-tip {
    display: none;
    position: absolute;
    left: 50%;
    bottom: calc(100% - 28px);
    transform: translateX(-50%);
    z-index: 2;
    width: max-content;
    padding: 8px 10px;
    border-radius: 10px;
    background: var(--ink);
    color: #fffaf3;
    font-size: 0.8rem;
    font-weight: 400;
    box-shadow: var(--shadow);
    pointer-events: none;
}
.trend-tip b, .trend-tip > span { display: flex; align-items: center; gap: 6px; }
.trend-col:hover .trend-tip, .trend-col:focus-visible .trend-tip { display: block; }
.trend-col:first-child .trend-tip { left: 0; transform: none; }
.trend-col:last-child .trend-tip { left: auto; right: 0; transform: none; }
.chart-note { margin: 10px 0 0; font-size: 0.82rem; }

.hbars { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.hbars li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; }
.hbar-value { font-variant-numeric: tabular-nums; white-space: nowrap; }
.hbar-value small { color: var(--muted); }
.hbar-track { grid-column: 1 / -1; height: 8px; border-radius: 4px; background: rgba(36, 28, 22, 0.07); }
.hbar-fill { display: block; height: 100%; min-width: 2px; border-radius: 4px; background: var(--teal-2); }

.due-strip {
    margin: 0 0 14px;
    padding: 10px 14px;
    border-radius: 12px;
    background: rgba(20, 63, 59, 0.08);
}

.calendar { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 8px; }
.dow {
    text-align: center;
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 600;
    padding-bottom: 4px;
}
.dow.is-weekend { color: var(--gold); }
.day {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    min-height: 104px;
    padding: 8px 10px;
    border-radius: 14px;
    background: #fff;
    border: 1px solid var(--line);
    text-decoration: none;
    color: inherit;
}
.day-pad { background: transparent; border-color: transparent; box-shadow: none; min-height: 0; }
.day-num { font-weight: 600; }
.mini { font-size: 0.75rem; font-variant-numeric: tabular-nums; line-height: 1.3; }
.mini-in { color: var(--income); }
.mini-out { color: var(--expense); }
.day.is-today { border-color: var(--teal-2); }
.day.is-selected { background: var(--teal); color: white; }
.day.is-selected .mini-in { color: #b7f3df; }
.day.is-selected .mini-out { color: #ffd0c2; }

.day-book { position: sticky; top: 16px; }
.txn-list { margin-top: 8px; }
.txn {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 4px 12px;
    padding: 12px 0;
    border-top: 1px solid var(--line);
}
.txn.is-editing { background: #f3f7f4; margin: 0 -8px; padding: 12px 8px; border-radius: 12px; }
.txn .row-actions { grid-column: 1 / -1; }
.txn-amt { margin: 0; font-variant-numeric: tabular-nums; font-weight: 600; }

.stack { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: 0.88rem; }
.field input, .field select, .inline-form input, .inline-form select {
    width: 100%;
    border: 1px solid var(--line);
    background: white;
    color: var(--ink);
    border-radius: 12px;
    padding: 10px 12px;
    font: inherit;
}
.field input:focus, .field select:focus, .inline-form input:focus, .inline-form select:focus {
    outline: 3px solid rgba(29, 92, 85, 0.18);
    border-color: var(--teal-2);
}

.field .pw-wrap { position: relative; display: block; font-size: inherit; color: inherit; }
.pw-wrap input { padding-right: 46px; }
.pw-toggle {
    position: absolute;
    top: 50%;
    right: 6px;
    transform: translateY(-50%);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}
.pw-toggle:hover { color: var(--teal-2); }
.pw-toggle:focus-visible { outline: 3px solid rgba(29, 92, 85, 0.18); color: var(--teal-2); }

.segment { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.segment label, .check {
    display: flex;
    gap: 8px;
    align-items: center;
}
.segment label {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 12px;
    background: white;
}
.segment input, .check input { width: auto; accent-color: var(--teal); }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    padding: 10px 16px;
    border: 1px solid transparent;
    background: white;
    color: var(--ink);
    font: inherit;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
}
.btn:hover { filter: brightness(0.98); }
.btn-primary { background: var(--teal); color: white; }
.btn-primary:hover { background: var(--teal-2); }
.btn-ghost { background: transparent; border-color: var(--line); }
.btn-danger { background: white; color: var(--expense); border-color: #f0c7b8; }
.btn-small { padding: 6px 10px; font-size: 0.85rem; }
.btn-sidebar {
    width: 100%;
    color: #f6f1e7;
    background: transparent;
    border-color: rgba(255, 255, 255, 0.22);
}
.auth-card .btn-primary { width: 100%; }

.badge {
    display: inline-block;
    margin-right: 6px;
    padding: 2px 8px;
    border-radius: 999px;
    background: #efe6d8;
    font-size: 0.78rem;
    font-weight: 600;
}
.badge-in { background: #e5f5ee; color: var(--income); }
.badge-out { background: #fde8df; color: var(--expense); }

.plain-list { list-style: none; padding: 0; margin: 16px 0 0; }
.plain-list li { padding: 10px 0; border-top: 1px solid var(--line); align-items: flex-start; }
.plain-list small { display: block; color: var(--muted); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 0.82rem; font-weight: 600; }
td.pos, td.neg { font-variant-numeric: tabular-nums; }

.flash-stack { display: grid; gap: 8px; margin-bottom: 12px; }
.flash {
    padding: 10px 12px;
    border-radius: 12px;
    margin-bottom: 12px;
}
.flash ul { margin: 0; padding-left: 18px; }
.flash-ok { background: #e5f5ee; color: #08573d; }
.flash-bad { background: #fde8df; color: #7c2d12; }

.pay-row, .inline-form { display: grid; gap: 10px; }
.pay-row {
    grid-template-columns: minmax(0, 1fr) minmax(240px, 1.1fr);
    padding: 14px 0;
    border-top: 1px solid var(--line);
}
.inline-form { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); align-items: end; }
.top-gap { margin-top: 18px; }

.auth-screen {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 1.15fr 0.85fr;
}
.brand-panel {
    padding: 64px;
    background:
        radial-gradient(500px 240px at 20% 10%, rgba(226, 196, 138, 0.25), transparent 60%),
        var(--sidebar);
    color: #f6f1e7;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}
.brand-panel h1 { font-size: 3rem; max-width: 12ch; }
.brand-kicker { color: #e2c48a; }
.brand-list { padding-left: 18px; }
.brand-list li { margin: 8px 0; }
.brand-foot { color: #d9c7a4; }
.auth-panel { display: grid; place-items: center; padding: 32px 20px; }
.auth-card { width: min(460px, 100%); }
.lede { color: var(--muted); }
.auth-links { display: flex; justify-content: space-between; gap: 12px; }
.reset-box {
    margin-bottom: 14px;
    padding: 12px;
    border-radius: 12px;
    background: #e5f5ee;
    word-break: break-all;
}
.setup-body { display: grid; place-items: center; padding: 32px 16px; }
.setup-body .auth-card { width: min(560px, 100%); }

@media (max-width: 1180px) {
    .cal-layout { grid-template-columns: 1fr; }
    .day-book { position: static; }
}

@media (max-width: 980px) {
    .shell { grid-template-columns: 1fr; }
    .sidebar { position: sticky; top: 0; z-index: 5; min-height: auto; }
    .nav, .sidebar-foot { display: none; }
    .shell.nav-open .nav, .shell.nav-open .sidebar-foot { display: flex; }
    .shell.nav-open .nav { flex-direction: column; }
    .nav-toggle {
        display: inline-flex;
        border: 1px solid rgba(255, 255, 255, 0.25);
        background: transparent;
        color: white;
        border-radius: 999px;
        padding: 6px 12px;
        font: inherit;
    }
    .cal-layout, .split, .pay-row, .auth-screen { grid-template-columns: 1fr; }
    .day-book { position: static; }
    .main { padding: 18px 14px 40px; }
    .brand-panel { padding: 32px 20px; }
    .brand-panel h1 { font-size: 2.2rem; }
}

@media (max-width: 720px) {
    h1 { font-size: 1.6rem; }
    .form-grid { grid-template-columns: 1fr; }
    .calendar { gap: 4px; }
    .day { min-height: 68px; padding: 6px; border-radius: 10px; }
    .mini { font-size: 0.68rem; }
    .head-actions .btn { padding: 8px 10px; }
}
