@import url('https://fonts.googleapis.com/css2?family=Kanit:wght@400;500;600&display=swap');

:root {
    --bg: #FAFAFA;
    --surface: #FFFFFF;
    --border: #E4E4E0;
    --ink: #202422;
    --ink-soft: #767B75;
    --accent: #3C6B4A;
    --danger: #B23B2E;
    --font-display: 'Kanit', system-ui, sans-serif;
    --font-body: 'Kanit', system-ui, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html {
    font-size: 18px;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font-body);
    font-weight: 400;
    -webkit-font-smoothing: antialiased;
    line-height: 1.5;
}

.page {
    max-width: 880px;
    margin: 0 auto;
    padding: 32px 20px 64px;
}

/* ---- top bar ---- */
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}

.brand {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 1.4rem;
    letter-spacing: -0.01em;
}

.brand-sub {
    font-size: 0.78rem;
    color: var(--ink-soft);
    margin-top: 3px;
}

.controls {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.76rem;
    padding-top: 4px;
}

/* ---- sync status pill ---- */
.sync-status {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-family: var(--font-body);
    font-size: 0.74rem;
    white-space: nowrap;
    padding: 5px 11px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--ink-soft);
    cursor: default;
}

.sync-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--ink-soft);
    flex-shrink: 0;
}

.sync-status[data-state="ok"] {
    color: var(--accent);
}

.sync-status[data-state="ok"] .sync-dot {
    background: var(--accent);
}

.sync-status[data-state="pending"] .sync-dot,
.sync-status[data-state="loading"] .sync-dot,
.sync-status[data-state="syncing"] .sync-dot {
    background: var(--ink-soft);
}

.sync-status[data-state="loading"] .sync-dot,
.sync-status[data-state="syncing"] .sync-dot {
    animation: sync-pulse 0.9s ease-in-out infinite;
}

.sync-status[data-state="error"],
.sync-status[data-state="offline"] {
    color: var(--danger);
}

.sync-status[data-state="error"] {
    cursor: pointer;
}

.sync-status[data-state="error"] .sync-dot,
.sync-status[data-state="offline"] .sync-dot {
    background: var(--danger);
}

@keyframes sync-pulse {
    50% {
        opacity: 0.25;
    }
}

@media (prefers-reduced-motion: reduce) {
    .sync-dot {
        animation: none !important;
    }
}

.reset-btn {
    font-family: var(--font-body);
    font-size: 0.74rem;
    background: none;
    border: 1px solid var(--border);
    padding: 6px 11px;
    border-radius: 6px;
    cursor: pointer;
    color: var(--ink-soft);
}

.reset-btn:hover {
    border-color: var(--danger);
    color: var(--danger);
}

.reset-btn:focus-visible,
.tab-btn:focus-visible,
input:focus-visible,
button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

/* ---- goal summary bar ---- */
.goal-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 18px;
}

.goal-bar-item {
    flex: 1;
    min-width: 150px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 9px 12px;
}

.goal-bar-label {
    display: block;
    font-size: 0.68rem;
    color: var(--ink-soft);
    margin-bottom: 3px;
}

.goal-bar-value {
    display: block;
    font-family: var(--font-mono);
    font-weight: 600;
    font-size: 0.92rem;
}

/* ---- timeline strip ---- */
.timeline-strip {
    margin-top: 18px;
}

.timeline-strip-title {
    font-size: 0.72rem;
    color: var(--ink-soft);
    margin-bottom: 6px;
}

.timeline-track {
    position: relative;
}

.timeline-line {
    position: absolute;
    left: 0;
    right: 0;
    top: 21px;
    height: 1px;
    background: var(--border);
}

.timeline-items {
    position: relative;
    display: flex;
    overflow-x: auto;
    padding-bottom: 4px;
}

.timeline-point {
    flex: 0 0 auto;
    min-width: 132px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    padding: 8px 6px 0;
    background: none;
    border: none;
    cursor: pointer;
    font-family: var(--font-body);
    color: var(--ink);
}

.timeline-dot {
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid var(--bg);
    box-shadow: 0 0 0 1px var(--border);
    flex-shrink: 0;
}

.timeline-label {
    font-size: 0.76rem;
    font-weight: 500;
    text-align: center;
    line-height: 1.25;
}

.timeline-age {
    font-size: 0.68rem;
    color: var(--ink-soft);
}

.timeline-sub {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--ink-soft);
}

.timeline-point:hover .timeline-label {
    color: var(--accent);
}

.timeline-point:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 4px;
}

.timeline-empty {
    font-size: 0.78rem;
    color: var(--ink-soft);
    padding: 8px 2px;
}

/* ---- timeline config modal ---- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(32, 36, 34, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 100;
}

.modal-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    width: 100%;
    max-width: 420px;
    max-height: 84vh;
    overflow-y: auto;
    padding: 18px 20px 20px;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
}

.modal-title {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 1.02rem;
}

.modal-close {
    background: none;
    border: 1px solid var(--border);
    color: var(--ink-soft);
    width: 26px;
    height: 26px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.78rem;
    flex-shrink: 0;
}

.modal-close:hover {
    border-color: var(--danger);
    color: var(--danger);
}

.modal-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
}

.modal-row label {
    font-size: 0.85rem;
    flex: 1;
}

.modal-row input[type=number],
.modal-row input[type=text] {
    width: 140px;
}

.modal-actions {
    margin-top: 16px;
    display: flex;
    justify-content: flex-end;
}

.modal-box-wide {
    max-width: 620px;
}

.modal-sub {
    font-size: 0.78rem;
    color: var(--ink-soft);
    margin: -6px 0 12px;
    line-height: 1.5;
}

.export-textarea {
    width: 100%;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.78rem;
    line-height: 1.5;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--ink);
    resize: vertical;
    white-space: pre-wrap;
}

/* ---- tabs ---- */
.tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin: 24px 0 0;
    border-bottom: 1px solid var(--border);
}

.tab-btn {
    font-family: var(--font-body);
    font-weight: 500;
    font-size: 0.8rem;
    padding: 9px 12px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    color: var(--ink-soft);
}

.tab-btn.active {
    color: var(--ink);
    font-weight: 600;
    border-color: var(--ink);
}

.panel {
    display: none;
    padding-top: 24px;
}

.panel.active {
    display: block;
}

/* ---- sub-tabs (nested within a panel) ---- */
.subtabs {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 20px;
}

.subtab-btn {
    font-family: var(--font-body);
    font-weight: 500;
    font-size: 0.78rem;
    padding: 6px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
    cursor: pointer;
    color: var(--ink-soft);
}

.subtab-btn.active {
    color: #fff;
    font-weight: 600;
    background: var(--accent);
    border-color: var(--accent);
}

.subtab-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.subpanel {
    display: none;
}

.subpanel.active {
    display: block;
}

/* ---- hero (dashboard) ---- */
.hero {
    padding: 4px 0;
}

.hero-label {
    font-size: 0.8rem;
    color: var(--ink-soft);
}

.hero-number {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 2.4rem;
    line-height: 1.15;
    margin-top: 4px;
}

.hero-number.negative {
    color: var(--danger);
}

.hero-number.positive {
    color: var(--accent);
}

.hero-sub {
    font-size: 0.82rem;
    color: var(--ink-soft);
    margin-top: 8px;
    max-width: 60ch;
}

.chip-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: 10px;
    margin-top: 22px;
}

.chip {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 14px 16px;
}

.chip-label {
    font-size: 0.72rem;
    color: var(--ink-soft);
    margin-bottom: 7px;
}

.chip-value {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 1.1rem;
}

.chart-box {
    margin-top: 24px;
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 14px;
    border-radius: 8px;
}

/* ---- savings-rate gauge ---- */
.gauge-box {
    margin-top: 24px;
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 14px;
    border-radius: 8px;
}

.gauge-wrap {
    position: relative;
    max-width: 320px;
    margin: 6px auto 0;
}

.savings-gauge-svg {
    width: 100%;
    display: block;
    touch-action: none;
    cursor: pointer;
}

.savings-gauge-svg:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 8px;
}

.gauge-track {
    fill: none;
    stroke: var(--border);
    stroke-width: 14;
    stroke-linecap: round;
}

.gauge-fill {
    fill: none;
    stroke: var(--accent);
    stroke-width: 14;
    stroke-linecap: round;
    transition: d 0.15s ease;
}

.gauge-target-line {
    stroke: var(--ink);
    stroke-width: 3;
    stroke-linecap: round;
}

.gauge-handle {
    fill: var(--surface);
    stroke: var(--ink);
    stroke-width: 3;
    cursor: grab;
}

.gauge-handle:active {
    cursor: grabbing;
}

.gauge-readout {
    position: absolute;
    left: 50%;
    top: 68%;
    transform: translateX(-50%);
    text-align: center;
}

.gauge-readout-main {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 1.7rem;
    line-height: 1.1;
}

.gauge-readout-sub {
    font-size: 0.68rem;
    color: var(--ink-soft);
    margin-top: 2px;
}

.gauge-readout-amt {
    font-family: var(--font-mono);
    font-size: 0.76rem;
    color: var(--ink-soft);
    margin-top: 1px;
}

.gauge-target-row {
    margin-top: 10px;
}

.gauge-target-row input[type=number] {
    width: 72px;
}

.chart-title {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 0.9rem;
    margin-bottom: 6px;
}

.chart-row {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
}

.chart-row .chart-box {
    flex: 1;
    min-width: 240px;
}

/* ---- ledger sections ---- */
.section-title {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 1rem;
    margin: 28px 0 4px;
}

.section-title:first-child {
    margin-top: 0;
}

.section-title.section-toggle {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    user-select: none;
}

.section-title.section-toggle:hover {
    color: var(--accent);
}

.section-title.section-toggle:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.section-title.section-toggle::after {
    content: "▾";
    font-size: 0.75rem;
    color: var(--ink-soft);
    flex-shrink: 0;
    transition: transform 0.15s ease;
}

.section-title.section-toggle.collapsed::after {
    transform: rotate(-90deg);
}

.section-title.section-toggle.collapsed {
    margin-bottom: 0;
}

.section-sub {
    font-size: 0.76rem;
    color: var(--ink-soft);
    margin-bottom: 10px;
}

.row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 9px 4px;
    border-bottom: 1px solid var(--border);
    gap: 14px;
}

.row-label {
    flex: 1;
    font-size: 0.9rem;
    min-width: 0;
}

.row-note {
    display: block;
    font-size: 0.7rem;
    color: var(--ink-soft);
    margin-top: 2px;
}

.row-value {
    font-family: var(--font-mono);
    text-align: right;
    white-space: nowrap;
    font-size: 0.9rem;
}

.row.total {
    font-weight: 600;
    border-top: 1px solid var(--ink);
    border-bottom: none;
    padding-top: 12px;
    margin-top: 2px;
}

.row.total .row-value {
    font-size: 1rem;
}

.row.link .row-value {
    color: var(--accent);
}

.row.link .row-label::after {
    content: " (ลิงก์)";
    font-size: 0.68rem;
    color: var(--ink-soft);
    font-family: var(--font-body);
}

.negative {
    color: var(--danger) !important;
}

.positive {
    color: var(--accent) !important;
}

input[type=number],
input[type=text] {
    font-family: var(--font-mono);
    font-size: 0.9rem;
    text-align: right;
    border: none;
    border-bottom: 1px solid var(--border);
    background: transparent;
    width: 112px;
    padding: 3px 4px;
    color: var(--ink);
}

input[type=text] {
    text-align: left;
    font-family: var(--font-body);
}

input:focus {
    outline: none;
    border-bottom: 1px solid var(--accent);
}

input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

input[type=number] {
    -moz-appearance: textfield;
}

input.pct {
    width: 64px;
}

.assumption-row .row-label {
    color: var(--ink-soft);
}

.dyn-row .row-value {
    display: flex;
    align-items: center;
    gap: 8px;
}

.row-del {
    background: none;
    border: 1px solid var(--border);
    color: var(--ink-soft);
    width: 22px;
    height: 22px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.68rem;
    line-height: 1;
    padding: 0;
    flex-shrink: 0;
}

.row-del:hover {
    border-color: var(--danger);
    color: var(--danger);
}

.add-row {
    border-bottom: 1px dashed var(--border);
    padding: 10px 4px;
    gap: 8px;
}

.add-row .add-label {
    flex: 1;
    border-bottom: 1px solid var(--border);
}

.add-row .add-value {
    width: 100px;
}

.add-btn {
    font-family: var(--font-body);
    font-weight: 500;
    font-size: 0.78rem;
    background: none;
    border: 1px solid var(--accent);
    color: var(--accent);
    padding: 6px 10px;
    border-radius: 6px;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
}

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

table.ledger-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 6px;
    font-size: 0.85rem;
}

table.ledger-table th {
    text-align: right;
    font-weight: 600;
    font-size: 0.72rem;
    color: var(--ink-soft);
    border-bottom: 1px solid var(--ink);
    padding: 6px 6px;
    font-family: var(--font-body);
}

table.ledger-table th:first-child,
table.ledger-table td:first-child {
    text-align: left;
}

table.ledger-table td {
    text-align: right;
    padding: 6px 6px;
    border-bottom: 1px solid var(--border);
    font-family: var(--font-mono);
}

/* ---- step-by-step calculation lines ---- */
.row.shop-row {
    flex-wrap: wrap;
}

.calc-line {
    flex-basis: 100%;
    font-family: var(--font-mono);
    font-size: 0.72rem;
    color: var(--ink-soft);
    padding-left: 2px;
    white-space: pre-wrap;
}

.calc-sum {
    padding: 10px 4px 0;
    font-size: 0.78rem;
    color: var(--ink);
}

/* ---- shopping plan ---- */
.shop-row {
    gap: 8px;
}

.shop-row .row-label {
    display: flex;
    align-items: center;
    gap: 8px;
}

.shop-row .shop-name {
    flex: 1;
    width: auto;
    min-width: 0;
}

.shop-row.bought .shop-name {
    text-decoration: line-through;
    color: var(--ink-soft);
}

.debt-note-input {
    flex-basis: 100%;
    width: 100%;
    margin-top: 4px;
    font-size: 0.78rem;
}

.shop-row .row-value {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: flex-end;
}

.shop-row input.shop-qty {
    width: 44px;
}

.shop-row input.shop-price {
    width: 90px;
}

.shop-sub {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    min-width: 84px;
    text-align: right;
}

.shop-fit {
    font-family: var(--font-body);
    font-size: 0.66rem;
    padding: 1px 6px;
    border-radius: 4px;
    border: 1px solid currentColor;
    white-space: nowrap;
}

select.shop-prio {
    font-family: var(--font-body);
    font-size: 0.78rem;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 5px;
    padding: 2px 4px;
    color: var(--ink);
}

input[type=checkbox].shop-check {
    width: 17px;
    height: 17px;
    accent-color: var(--accent);
    flex-shrink: 0;
}

.switch {
    position: relative;
    display: inline-block;
    width: 34px;
    height: 20px;
    flex-shrink: 0;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.switch .slider {
    position: absolute;
    inset: 0;
    cursor: pointer;
    background: var(--border);
    border-radius: 20px;
    transition: background 0.15s ease;
}

.switch .slider::before {
    content: "";
    position: absolute;
    width: 14px;
    height: 14px;
    left: 3px;
    top: 3px;
    background: #fff;
    border-radius: 50%;
    transition: transform 0.15s ease;
}

.switch input:checked + .slider {
    background: var(--accent);
}

.switch input:checked + .slider::before {
    transform: translateX(14px);
}

.row.dyn-row.row-disabled {
    opacity: 0.5;
}

input[type=month] {
    font-family: var(--font-mono);
    font-size: 0.88rem;
    border: none;
    border-bottom: 1px solid var(--border);
    background: transparent;
    color: var(--ink);
}

#in-shop-budget {
    margin-left: 6px;
}

.note-block {
    font-size: 0.78rem;
    color: var(--ink-soft);
    margin: 14px 2px 6px;
    max-width: 64ch;
}

footer {
    margin-top: 40px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
    font-size: 0.72rem;
    color: var(--ink-soft);
    max-width: 64ch;
}

@media (max-width:560px) {
    .hero-number {
        font-size: 1.9rem;
    }

    .row {
        flex-wrap: wrap;
    }

    .row-label {
        flex-basis: 100%;
    }

    .row-value {
        margin-left: auto;
    }

    table.ledger-table {
        font-size: 0.76rem;
    }

    .chart-row {
        flex-direction: column;
    }
}
