:root {
    color-scheme: dark light;
    /* Claude-style warm dark palette */
    --bg:        #1f1e1d;
    --surface:   #2a2825;
    --surface-2: #34312d;
    --border:    #44413d;
    --text:      #f4f3ee;
    --muted:     #9b9a96;
    --accent:    #cc785c;   /* Claude coral */
    --accent-2:  #b56a51;   /* coral hover/pressed */
    --accent-rgb: 204,120,92;
    --success: #4ade80;
    --danger:  #ef5350;
    --warning: #f59e0b;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

@media (prefers-color-scheme: light) {
    :root {
        --bg:        #faf9f5;
        --surface:   #ffffff;
        --surface-2: #f3f0ea;
        --border:    #e7e3da;
        --text:      #1f1e1d;
        --muted:     #6e6c66;
        --accent:    #cc785c;
        --accent-2:  #a85d44;
        --shadow: 0 8px 24px rgba(31, 30, 29, 0.08);
    }
}

/* Manual theme overrides via Settings */
html.force-light {
    color-scheme: light;
    --bg:        #faf9f5;
    --surface:   #ffffff;
    --surface-2: #f3f0ea;
    --border:    #e7e3da;
    --text:      #1f1e1d;
    --muted:     #6e6c66;
    --accent:    #cc785c;
    --accent-2:  #a85d44;
    --shadow: 0 8px 24px rgba(31, 30, 29, 0.08);
}
html.force-dark {
    color-scheme: dark;
    --bg:        #1f1e1d;
    --surface:   #2a2825;
    --surface-2: #34312d;
    --border:    #44413d;
    --text:      #f4f3ee;
    --muted:     #9b9a96;
    --accent:    #cc785c;
    --accent-2:  #b56a51;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 16px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.container {
    max-width: 720px;
    margin: 0 auto;
    padding: 16px max(16px, env(safe-area-inset-right)) calc(72px + max(2px, calc(env(safe-area-inset-bottom) - 16px))) max(16px, env(safe-area-inset-left));
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    padding-top: max(12px, env(safe-area-inset-top));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-bottom: 12px;
    padding-left: max(16px, env(safe-area-inset-left));
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.topbar h1 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
}

.topbar .month-nav {
    display: flex;
    align-items: center;
    gap: 8px;
}

.topbar .month-nav button {
    padding: 6px 10px;
    font-size: 0.95rem;
}

/* Bottom nav */
.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    display: flex;
    z-index: 20;
    padding-bottom: max(2px, calc(env(safe-area-inset-bottom) - 16px));
}

.bottom-nav a {
    flex: 1;
    text-align: center;
    padding: 6px 4px 4px;
    color: var(--muted);
    font-size: 0.74rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    text-decoration: none;
}

.bottom-nav a.active {
    color: var(--accent);
}

.bottom-nav .ico {
    font-size: 1.25rem;
    line-height: 1;
}

/* Cards */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: var(--shadow);
}

.card h2 {
    margin: 0 0 12px;
    font-size: 1rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* Stats */
.stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-bottom: 12px;
}

.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    text-align: center;
}

.stat .label {
    font-size: 0.72rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.stat .value {
    font-size: 1.05rem;
    font-weight: 700;
    margin-top: 4px;
    word-break: break-word;
}

.stat.income .value { color: var(--success); }
.stat.expense .value { color: var(--danger); }
.stat.net .value.neg { color: var(--danger); }
.stat.net .value.pos { color: var(--success); }

/* Forms */
form .row {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 12px;
}

label {
    font-size: 0.85rem;
    color: var(--muted);
}

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

input[type=text],
input[type=password],
input[type=number],
input[type=date],
input[type=email],
input[type=search],
input[type=tel],
input[type=url],
select,
textarea {
    width: 100%;
    box-sizing: border-box;
    padding: 10px 12px;
    min-height: 44px;
    line-height: 1.4;
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    outline: none;
    font: inherit;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

/* Search input — strip native magnifier and clear button on Safari/macOS */
input[type=search]::-webkit-search-decoration,
input[type=search]::-webkit-search-cancel-button,
input[type=search]::-webkit-search-results-button,
input[type=search]::-webkit-search-results-decoration { -webkit-appearance: none; }

input[type=search] {
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%239b9a96' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: 12px center;
    padding-left: 36px;
}

/* Number input spinner — make the up/down arrows visible on dark theme.
 * Webkit (Chrome/Safari) and Firefox tint these from the input's color, but only
 * when color-scheme matches. Force a custom render to be safe across browsers. */
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
    opacity: 1;
    filter: var(--icon-filter, invert(85%) sepia(10%) hue-rotate(0deg));
}
@media (prefers-color-scheme: light) {
    :root { --icon-filter: invert(30%); }
}
html.force-light { --icon-filter: invert(30%); }
html.force-dark  { --icon-filter: invert(85%); }

select {
    /* custom chevron — keeps native picker, removes the iOS stock arrow box */
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%238aa0c2' d='M0 0l6 8 6-8z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
}

input[type=date] {
    /* iOS Safari pads date inputs differently — force same height */
    min-height: 44px;
}

input[type=color] {
    padding: 4px;
}

textarea {
    min-height: 88px;
    line-height: 1.5;
}

input:focus, select:focus, textarea:focus {
    border-color: var(--accent);
}

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

button, .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 16px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-weight: 600;
    text-decoration: none;
}

button:hover, .btn:hover { background: var(--accent-2); text-decoration: none; }
button.secondary, .btn.secondary {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
}
button.secondary:hover, .btn.secondary:hover { background: var(--border); }
button.danger, .btn.danger { background: var(--danger); }
button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }

button.full, .btn.full { width: 100%; }

/* Segmented control */
.segmented {
    display: flex;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: var(--surface-2);
}

.segmented label {
    flex: 1;
    text-align: center;
    padding: 10px;
    cursor: pointer;
    color: var(--muted);
}

.segmented input { display: none; }
.segmented input:checked + span {
    background: var(--accent);
    color: #fff;
    display: block;
    margin: -10px;
    padding: 10px;
}

/* Transaction list */
.tx-list { display: flex; flex-direction: column; gap: 6px; }

.tx-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.tx-item .icon {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    flex-shrink: 0;
    color: #fff;
}

.tx-item .body { flex: 1; min-width: 0; }
.tx-item .body .title {
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.tx-item .body .meta {
    font-size: 0.78rem;
    color: var(--muted);
}

.tx-item .amount {
    font-weight: 700;
    flex-shrink: 0;
}
.tx-item.income .amount { color: var(--success); }
.tx-item.expense .amount { color: var(--danger); }

.tx-day-header {
    display: flex;
    justify-content: space-between;
    padding: 14px 4px 6px;
    font-size: 0.82rem;
    color: var(--muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* FAB */
.fab {
    position: fixed;
    right: max(16px, env(safe-area-inset-right));
    bottom: 64px;
    bottom: calc(56px + max(2px, calc(env(safe-area-inset-bottom) - 16px)));
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    font-size: 1.7rem;
    line-height: 1;
    box-shadow: 0 6px 20px rgba(var(--accent-rgb), 0.45);
    z-index: 30;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Auth pages */
.auth-wrap {
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: max(20px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
}
.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    box-shadow: var(--shadow);
}
.auth-card h1 { margin: 0 0 4px; font-size: 1.3rem; }
.auth-card .lead { color: var(--muted); margin: 0 0 20px; font-size: 0.95rem; }

/* Flash */
.flash {
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    margin-bottom: 12px;
    font-size: 0.92rem;
    border: 1px solid;
}
.flash.success { background: rgba(34, 197, 94, 0.1); border-color: rgba(34, 197, 94, 0.4); color: var(--success); }
.flash.error { background: rgba(239, 68, 68, 0.1); border-color: rgba(239, 68, 68, 0.4); color: var(--danger); }
.flash.info { background: rgba(var(--accent-rgb), 0.1); border-color: rgba(var(--accent-rgb), 0.4); color: var(--accent); }

/* List rows */
.list-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 6px;
}
.list-row .swatch {
    width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0;
}
.list-row .body { flex: 1; min-width: 0; }
.list-row .body .title { font-weight: 500; }
.list-row .body .meta { font-size: 0.78rem; color: var(--muted); }
.list-row .actions { display: flex; gap: 6px; flex-shrink: 0; }
.list-row .actions a, .list-row .actions button {
    padding: 6px 10px; font-size: 0.85rem;
}

/* Empty state */
.empty {
    text-align: center;
    padding: 32px 16px;
    color: var(--muted);
}

/* Header actions */
.section-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 8px 0 12px;
}
.section-head h2 { margin: 0; font-size: 1.05rem; }

/* Utility */
.muted { color: var(--muted); }
.text-right { text-align: right; }
.mb-0 { margin-bottom: 0; }
.mb-2 { margin-bottom: 12px; }
.mt-2 { margin-top: 12px; }
.mt-3 { margin-top: 20px; }
.gap-2 { gap: 8px; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.between { justify-content: space-between; }
.center { align-items: center; }

/* Helper hover hint */
.hint { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }

/* Top progress bar (preloader) */
.preloader {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0;
    background: var(--accent);
    box-shadow: 0 0 10px rgba(var(--accent-rgb), .8);
    z-index: 9999;
    transition: width .25s ease-out, opacity .2s;
    opacity: 0;
    pointer-events: none;
    border-radius: 0 2px 2px 0;
}
.preloader.active { opacity: 1; }

/* Submit-disabled state — visual feedback when a form is processing */
button[disabled], .btn[disabled], button.is-loading {
    opacity: 0.65;
    cursor: progress;
    pointer-events: none;
}
button.is-loading::after,
.btn.is-loading::after {
    content: '';
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-left: 8px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin .8s linear infinite;
    vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* SweetAlert2 — match site palette */
.sw-popup {
    background: var(--surface) !important;
    color: var(--text) !important;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.sw-popup .swal2-title { color: var(--text); font-size: 1.1rem; font-weight: 600; }
.sw-popup .swal2-html-container { color: var(--muted); font-size: .95rem; }
.sw-popup .swal2-icon { transform: scale(.85); }
.sw-popup .swal2-actions { gap: 8px; margin-top: 14px; }

.sw-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 18px;
    border-radius: var(--radius-sm);
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    border: none;
    min-height: 0;
}
.sw-btn-primary   { background: var(--accent);   color: #fff; }
.sw-btn-primary:hover { background: var(--accent-2); }
.sw-btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.sw-btn-secondary:hover { background: var(--border); }
.sw-btn-danger    { background: var(--danger);   color: #fff; }
.sw-btn-danger:hover { filter: brightness(1.05); }

/* Toast variant — minimal padding, top-aligned */
.sw-toast {
    background: var(--surface) !important;
    color: var(--text) !important;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    margin-top: max(20px, env(safe-area-inset-top)) !important;
}
.sw-toast .swal2-title { color: var(--text); font-size: .92rem; }

/* File dropzone (receipt upload) */
.dropzone {
    position: relative;
    display: block;
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    background: var(--surface-2);
    padding: 28px 16px;
    text-align: center;
    cursor: pointer;
    transition: border-color .15s, background .15s;
    user-select: none;
    -webkit-user-select: none;
}

.dropzone:hover, .dropzone:focus-within {
    border-color: var(--accent);
    background: color-mix(in srgb, var(--accent) 8%, var(--surface-2));
}

.dropzone.has-file {
    padding: 12px;
    text-align: left;
    border-style: solid;
    cursor: default;
}

.dropzone input[type=file] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
    min-height: 0;
    padding: 0;
    border: none;
}

.dropzone .dz-empty .icon {
    font-size: 2.4rem;
    line-height: 1;
    margin-bottom: 8px;
    display: block;
}

.dropzone .dz-empty .title {
    font-weight: 600;
    color: var(--text);
}

.dropzone .dz-empty .sub {
    font-size: .85rem;
    color: var(--muted);
    margin-top: 4px;
}

.dropzone .dz-actions {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-top: 14px;
}

.dropzone .dz-actions button {
    padding: 8px 14px;
    font-size: .9rem;
}

.dropzone .dz-preview {
    display: none;
    align-items: center;
    gap: 12px;
}
.dropzone.has-file .dz-empty { display: none; }
.dropzone.has-file .dz-preview { display: flex; }

.dropzone .dz-thumb {
    width: 64px;
    height: 64px;
    border-radius: 8px;
    object-fit: cover;
    flex-shrink: 0;
    background: var(--bg);
    border: 1px solid var(--border);
}

.dropzone .dz-meta {
    flex: 1;
    min-width: 0;
}
.dropzone .dz-meta .name {
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.dropzone .dz-meta .size { font-size: .78rem; color: var(--muted); }

.dropzone .dz-clear {
    flex-shrink: 0;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--muted);
    padding: 6px 10px;
    font-size: .85rem;
    border-radius: 6px;
    cursor: pointer;
    min-height: 0;
}
.dropzone .dz-clear:hover { background: var(--surface); color: var(--text); }

/* Live camera */
.cam-stage {
    display: none;
    flex-direction: column;
    gap: 12px;
}
.cam-stage.active { display: flex; }

.cam-frame {
    position: relative;
    width: 100%;
    aspect-ratio: 3 / 4;
    border-radius: 12px;
    overflow: hidden;
    background: #000;
}
.cam-frame video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
.cam-guide {
    position: absolute;
    inset: 8% 8% 14% 8%;
    border: 2px dashed rgba(255,255,255,.65);
    border-radius: 6px;
    pointer-events: none;
}
.cam-overlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}
.cam-pages {
    top: auto !important;
    bottom: 8px;
    left: auto !important;
    right: 8px;
}
.cam-status[data-tone="yellow"] { background: rgba(202,138,4,.85); }
.cam-status[data-tone="green"]  { background: rgba(22,163,74,.85); }
.cam-status[data-tone="red"]    { background: rgba(220,38,38,.85); }
.cam-flash {
    position: absolute;
    inset: 0;
    background: #fff;
    opacity: 0;
    pointer-events: none;
    transition: opacity .15s;
}
.cam-flash.fire { opacity: .8; transition: opacity .04s; }

.cam-status {
    position: absolute;
    top: 8px;
    left: 8px;
    background: rgba(0,0,0,.55);
    color: #fff;
    padding: 4px 10px;
    border-radius: 999px;
    font-size: .78rem;
    font-weight: 600;
}

.cam-controls {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 12px;
}
.cam-controls .left  { justify-self: start; }
.cam-controls .right { justify-self: end;   }

.cam-shutter {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: var(--accent);
    border: 4px solid #fff;
    box-shadow: 0 0 0 2px var(--accent), 0 6px 16px rgba(var(--accent-rgb), .35);
    cursor: pointer;
    padding: 0;
    min-height: 0;
    color: transparent;
}
.cam-shutter:active { transform: scale(0.96); }
.cam-shutter:disabled { opacity: 0.5; }

/* Multi-page thumbnails */
.pages-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 8px;
}
.page-thumb {
    position: relative;
    aspect-ratio: 3 / 4;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border);
    background: var(--surface);
}
.page-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
.page-thumb .badge {
    position: absolute;
    top: 4px;
    left: 4px;
    background: var(--accent);
    color: #fff;
    font-size: .72rem;
    font-weight: 700;
    padding: 2px 6px;
    border-radius: 999px;
    line-height: 1;
}
.page-thumb .remove {
    position: absolute;
    top: 4px;
    right: 4px;
    background: rgba(0,0,0,.7);
    color: #fff;
    border: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
    padding: 0;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
.page-thumb .remove:hover { background: rgba(0,0,0,.9); }
