/* ==========================================================================
   無量簿 Immeasurable Ledger — 樣式
   ========================================================================== */

:root {
    color-scheme: light dark;

    --bg: #f3f6f4;
    --surface: #ffffff;
    --surface-2: #edf1ee;
    --text: #17211c;
    --text-muted: #64706a;
    --line: #dde4e0;
    --accent: #2f7d5b;
    --accent-strong: #266349;
    --accent-soft: #e3f1ea;
    --on-accent: #ffffff;
    --danger: #b3261e;
    --danger-soft: #fbeae8;
    --shadow: 0 10px 30px rgba(16, 40, 28, 0.08);
    --radius: 16px;
    --font: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #101512;
        --surface: #191f1b;
        --surface-2: #232b26;
        --text: #e8efea;
        --text-muted: #9aa8a0;
        --line: #2c3630;
        --accent: #63c495;
        --accent-strong: #86d9ae;
        --accent-soft: #1d3229;
        --on-accent: #0c1a13;
        --danger: #ff8a80;
        --danger-soft: #341d1b;
        --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    }
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html {
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

button,
input,
select,
textarea {
    font: inherit;
    color: inherit;
}

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

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

.app {
    max-width: 680px;
    margin: 0 auto;
    padding: 22px 16px calc(28px + env(safe-area-inset-bottom));
}

/* ---------- 標題 / 頁籤 ---------- */

.app-header {
    display: flex;
    align-items: baseline;
    gap: 10px;
    margin: 0 2px 18px;
}

.app-header h1 {
    margin: 0;
    font-size: 1.55rem;
    letter-spacing: 0.03em;
}

.app-header .sub {
    font-size: 0.78rem;
    color: var(--text-muted);
    letter-spacing: 0.04em;
}

.tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    padding: 4px;
    margin-bottom: 18px;
    background: var(--surface-2);
    border-radius: 999px;
}

.tab {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 12px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--text-muted);
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.18s ease, color 0.18s ease, box-shadow 0.18s ease;
}

.tab[aria-selected="true"] {
    background: var(--surface);
    color: var(--text);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12);
}

.badge {
    min-width: 1.6em;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--accent-strong);
    font-size: 0.72rem;
    font-weight: 700;
    line-height: 1.6;
    text-align: center;
}

/* ---------- 卡片 / 表單 ---------- */

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

.amount-field {
    display: flex;
    align-items: center;
    margin-bottom: 18px;
    padding: 8px 16px;
    background: var(--surface-2);
    border: 1px solid transparent;
    border-radius: 14px;
    transition: border-color 0.18s ease, background-color 0.18s ease;
}

.amount-field:focus-within {
    background: var(--surface);
    border-color: var(--accent);
}

.amount-field.is-invalid {
    border-color: var(--danger);
}

.amount-field input {
    width: 100%;
    min-width: 0;
    padding: 4px 0;
    border: 0;
    background: transparent;
    font-size: 2rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
}

.amount-field input::placeholder {
    color: var(--text-muted);
    opacity: 0.45;
}

.amount-field input:focus {
    outline: none;
}

.field {
    margin-bottom: 16px;
}

.field > label,
.field-label {
    display: block;
    margin-bottom: 6px;
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.03em;
}

.field:last-child {
    margin-bottom: 0;
}

.input,
.field input,
.field textarea {
    width: 100%;
    padding: 11px 13px;
    background: var(--surface-2);
    border: 1px solid transparent;
    border-radius: 11px;
    transition: border-color 0.18s ease, background-color 0.18s ease;
}

.field textarea {
    display: block;
    resize: vertical;
    min-height: 76px;
}

.input:focus,
.field input:focus,
.field textarea:focus {
    background: var(--surface);
    border-color: var(--accent);
    outline: none;
}

.field input.is-invalid {
    border-color: var(--danger);
}

.chip-row {
    display: flex;
    gap: 8px;
    padding: 2px 0 6px;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
}

.chip-row::-webkit-scrollbar {
    display: none;
}

.chip {
    flex: 0 0 auto;
    padding: 8px 15px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 999px;
    color: var(--text);
    font-size: 0.9rem;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.chip:hover {
    border-color: var(--accent);
}

.chip.is-selected {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--on-accent);
    font-weight: 700;
}

.chip-add {
    color: var(--text-muted);
    border-style: dashed;
}

/* 備註收合 */

.more {
    margin-top: 4px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
}

.more > summary {
    display: flex;
    align-items: center;
    gap: 7px;
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 600;
    list-style: none;
    cursor: pointer;
    user-select: none;
}

.more > summary::-webkit-details-marker {
    display: none;
}

.more > summary::before {
    content: "＋";
    font-weight: 400;
}

.more[open] > summary::before {
    content: "－";
}

.more-body {
    padding-top: 14px;
}

.switch {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--text-muted);
    font-size: 0.85rem;
    cursor: pointer;
    user-select: none;
}

.switch input {
    width: 17px;
    height: 17px;
    margin: 0;
    accent-color: var(--accent);
}

/* ---------- 按鈕 ---------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 11px 18px;
    border: 1px solid transparent;
    border-radius: 12px;
    background: transparent;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s ease, opacity 0.15s ease, transform 0.06s ease;
}

.btn:active {
    transform: translateY(1px);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--accent);
    color: var(--on-accent);
}

.btn-primary:hover {
    background: var(--accent-strong);
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}

.btn-ghost {
    border-color: var(--line);
    color: var(--text);
}

.btn-ghost:hover {
    background: var(--surface-2);
}

.btn-danger-ghost {
    color: var(--danger);
}

.btn-danger-ghost:hover {
    background: var(--danger-soft);
}

.btn-sm {
    padding: 8px 13px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    font-size: 0.85rem;
}

.btn-sm:hover {
    background: var(--surface-2);
}

.save-bar {
    position: sticky;
    bottom: calc(10px + env(safe-area-inset-bottom));
    margin-top: 8px;
    padding-top: 10px;
    background: linear-gradient(to top, var(--surface) 58%, rgba(0, 0, 0, 0));
}

.save-bar .btn-primary {
    width: 100%;
    padding: 15px;
    border-radius: 14px;
    font-size: 1rem;
}

/* ---------- 記錄列表 ---------- */

.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}

.toolbar .search {
    flex: 1 1 100%;
}

.toolbar .spacer {
    flex: 1 1 auto;
}

.summary {
    margin: 0 0 12px;
    color: var(--text-muted);
    font-size: 0.8rem;
}

.cat {
    margin-bottom: 12px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    overflow: hidden;
}

.cat > summary {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    list-style: none;
    cursor: pointer;
    user-select: none;
}

.cat > summary::-webkit-details-marker {
    display: none;
}

.cat > summary::after {
    content: "";
    flex: 0 0 auto;
    width: 8px;
    height: 8px;
    margin-left: 2px;
    border-right: 2px solid var(--text-muted);
    border-bottom: 2px solid var(--text-muted);
    transform: rotate(-45deg);
    transition: transform 0.18s ease;
}

.cat[open] > summary::after {
    transform: rotate(45deg);
}

.cat-name {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    font-weight: 700;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.cat-meta {
    flex: 0 0 auto;
    color: var(--text-muted);
    font-size: 0.76rem;
    white-space: nowrap;
}

.entries {
    border-top: 1px solid var(--line);
}

.entry {
    display: block;
    width: 100%;
    padding: 12px 16px;
    border: 0;
    border-top: 1px solid var(--line);
    background: transparent;
    text-align: left;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.entries .entry:first-child {
    border-top: 0;
}

.entry:hover,
.entry:focus-visible {
    background: var(--surface-2);
}

.entry-top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
}

.entry-amount {
    min-width: 0;
    overflow-wrap: anywhere;
    font-size: 1.05rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.entry-amount .unit {
    margin-left: 5px;
    color: var(--text-muted);
    font-size: 0.78rem;
    font-weight: 500;
}

.entry-time {
    flex: 0 0 auto;
    color: var(--text-muted);
    font-size: 0.74rem;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.entry-text {
    display: block;
    margin-top: 2px;
    font-size: 0.93rem;
    overflow-wrap: anywhere;
}

.entry-extra {
    display: block;
    margin-top: 4px;
    color: var(--text-muted);
    font-size: 0.79rem;
    overflow-wrap: anywhere;
}

.empty {
    padding: 44px 16px;
    color: var(--text-muted);
    text-align: center;
}

.empty p {
    margin: 0 0 6px;
}

.empty-sub {
    font-size: 0.85rem;
    opacity: 0.8;
}

/* ---------- 對話框 ---------- */

.dialog {
    width: min(560px, calc(100vw - 32px));
    max-height: min(88vh, 800px);
    padding: 0;
    border: 0;
    border-radius: 18px;
    background: var(--surface);
    color: var(--text);
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.3);
}

.dialog::backdrop {
    background: rgba(8, 14, 10, 0.5);
}

.dialog[open] {
    animation: dialog-pop 0.16s ease-out;
}

@keyframes dialog-pop {
    from {
        opacity: 0.6;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

.dialog form {
    display: flex;
    flex-direction: column;
    max-height: inherit;
}

.dialog-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 16px 18px 6px;
}

.dialog-head h2 {
    margin: 0;
    font-size: 1.05rem;
}

.dialog-body {
    flex: 1 1 auto;
    padding: 8px 18px 4px;
    overflow-y: auto;
}

.dialog-text {
    margin: 4px 0 10px;
    font-size: 0.92rem;
    overflow-wrap: anywhere;
}

.dialog-text.muted {
    color: var(--text-muted);
    font-size: 0.82rem;
}

.dialog-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
    padding: 12px 18px calc(16px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
}

.dialog-actions .spacer {
    flex: 1 1 auto;
}

.dialog-actions .btn {
    width: auto;
}

.dialog-actions-stack {
    flex-direction: column;
    align-items: stretch;
}

.dialog-actions-stack .btn {
    width: 100%;
}

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--text-muted);
    font-size: 1rem;
    cursor: pointer;
}

.icon-btn:hover {
    background: var(--surface-2);
    color: var(--text);
}

.field-error {
    margin: 0 0 8px;
    color: var(--danger);
    font-size: 0.82rem;
}

.meta {
    margin: 14px 0 0;
    padding-top: 12px;
    border-top: 1px solid var(--line);
    color: var(--text-muted);
    font-size: 0.78rem;
    overflow-wrap: anywhere;
}

/* ---------- Toast ---------- */

.toast {
    position: fixed;
    left: 50%;
    bottom: calc(22px + env(safe-area-inset-bottom));
    z-index: 50;
    display: flex;
    align-items: center;
    gap: 16px;
    max-width: min(520px, calc(100vw - 32px));
    padding: 12px 16px;
    border-radius: 13px;
    background: #21282a;
    color: #f2f6f4;
    font-size: 0.88rem;
    box-shadow: 0 14px 34px rgba(0, 0, 0, 0.32);
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 12px);
    transition: opacity 0.18s ease, transform 0.18s ease;
}

.toast.is-visible {
    opacity: 1;
    pointer-events: auto;
    transform: translate(-50%, 0);
}

.toast button {
    flex: 0 0 auto;
    padding: 4px 2px;
    border: 0;
    background: none;
    color: #8ee7b9;
    font-weight: 700;
    cursor: pointer;
}

.notice {
    margin: 0 0 14px;
    padding: 10px 13px;
    border: 1px solid var(--line);
    border-radius: 11px;
    background: var(--surface-2);
    color: var(--text-muted);
    font-size: 0.82rem;
}

/* ---------- 手機 ---------- */

@media (max-width: 600px) {
    .app {
        padding: 16px 12px calc(22px + env(safe-area-inset-bottom));
    }

    .app-header h1 {
        font-size: 1.35rem;
    }

    .card {
        padding: 15px;
    }

    .amount-field input {
        font-size: 1.8rem;
    }

    .dialog.sheet {
        width: 100%;
        max-width: none;
        max-height: 92vh;
        margin: auto auto 0;
        border-radius: 18px 18px 0 0;
    }

    .dialog.sheet[open] {
        animation: sheet-up 0.2s ease-out;
    }

    @keyframes sheet-up {
        from {
            opacity: 0.8;
            transform: translateY(28px);
        }
        to {
            opacity: 1;
            transform: none;
        }
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
}
