*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #1e1e1e;
    --surface: #2a2a2a;
    --surface2: #333;
    --border: #3d3d3d;
    --accent: #7c3aed;
    --accent-dim: #6d28d9;
    --accent-glow: rgba(124,58,237,0.15);
    --amber: #d4a054;
    --text: #e8e8e8;
    --text2: #999;
    --text3: #666;
    --white: #2a2a2a;
    --red: #f87171;
    --green: #34d399;
    --sidebar-w: 240px;
}

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

body {
    font-family: 'Inter', system-ui, sans-serif;
    background: var(--bg);
    background-image: radial-gradient(ellipse at 50% 0%, rgba(124,58,237,0.04) 0%, transparent 50%);
    color: var(--text);
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ====== AD ====== */
.ad-banner {
    width: 100%;
    background: #171717;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    padding: 4px 8px;
    text-align: center;
}
.ad-placeholder {
    display: inline-block;
    max-width: 728px; width: 100%;
    height: 40px; line-height: 40px;
    color: var(--text3); font-size: 9px;
    letter-spacing: 1px;
    border: 1px dashed var(--border);
    border-radius: 3px;
}

/* ====== SIDEBAR ====== */
.sidebar {
    position: fixed; left: 0; top: 0;
    width: var(--sidebar-w); height: 100dvh;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    z-index: 100; overflow-y: auto;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 20px rgba(0,0,0,0.3);
}
.sidebar.open { transform: translateX(0); }
.sidebar-header {
    padding: 14px;
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
}
.sidebar-close {
    background: none; border: none;
    color: var(--text2); font-size: 24px; cursor: pointer;
    transition: color 0.15s;
}
.sidebar-close:hover { color: #e8e8e8; }
.sidebar-title {
    font-size: 13px; font-weight: 600;
    color: var(--text); text-transform: uppercase; letter-spacing: 0.5px;
}
.nav-list { list-style: none; padding: 8px; }
.nav-divider {
    font-size: 9px; text-transform: uppercase;
    letter-spacing: 1px; color: var(--text2);
    padding: 10px 10px 4px; font-weight: 600;
}
.nav-item {
    display: flex; align-items: center; gap: 8px;
    padding: 9px 10px; border-radius: 6px;
    cursor: pointer; font-size: 13px;
    color: var(--text2); transition: all 0.15s;
    -webkit-tap-highlight-color: transparent;
}
.nav-item:hover { background: var(--surface); color: var(--text); }
.nav-item.active { background: rgba(124,58,237,0.08); color: var(--accent); border-left: 2px solid var(--accent); }
.nav-icon {
    width: 24px; height: 24px;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; opacity: 0.5;
}
.nav-item:hover .nav-icon { opacity: 0.8; }
.nav-item.active .nav-icon { opacity: 1; }
.sidebar-saved { padding: 8px; border-top: 1px solid var(--border); margin-top: auto; }
.saved-list { max-height: 200px; overflow-y: auto; }
.saved-empty { font-size: 11px; color: var(--text2); padding: 12px 10px; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.saved-empty svg { opacity: 0.4; }
.saved-item {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 6px; padding: 7px 9px; margin-bottom: 4px;
    cursor: pointer; position: relative;
}
.saved-item:hover { border-color: var(--accent); }
.saved-item.active { border-color: var(--accent); background: rgba(124,58,237,0.08); }
.saved-name { font-size: 11px; font-weight: 600; color: var(--text); }
.saved-date { font-size: 9px; color: var(--text2); }
.saved-result { font-size: 10px; color: var(--accent); font-weight: 600; }
.saved-delete {
    position: absolute; top: 5px; right: 5px;
    background: none; border: none; color: var(--text2);
    font-size: 13px; cursor: pointer; line-height: 1;
}
.saved-delete:hover { color: var(--red); }

/* ====== OVERLAY ====== */
.sidebar-overlay {
    display: none; position: fixed; inset: 0;
    background: rgba(0,0,0,0.5); z-index: 90;
}
.sidebar-overlay.open { display: block; }

/* ====== MAIN ====== */
.main { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.topbar {
    height: 38px; display: flex; align-items: center;
    padding: 0 10px; border-bottom: 1px solid var(--border);
    flex-shrink: 0; background: var(--surface);
}
.menu-btn {
    background: none; border: none; color: var(--text);
    cursor: pointer; margin-right: 8px; padding: 2px;
    display: flex; align-items: center;
    -webkit-tap-highlight-color: transparent;
}
.page-title {
    font-size: 13px; font-weight: 600; color: var(--text);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.content {
    flex: 1; padding: 6px;
    display: flex; flex-direction: column;
    min-height: 0; overflow: hidden;
}

/* ====== PANELS ====== */
.calc-panel { display: none; }
.calc-panel.active { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.calc-grid { display: flex; flex-direction: column-reverse; flex: 1; min-height: 0; gap: 6px; }

/* ====== CARDS ====== */
.glass {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
}

/* ====== INPUT CARD ====== */
.input-card {
    padding: 8px;
    flex-shrink: 0;
}
.input-card h3 { display: none; }
.input-fields {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px 8px;
}
.input-card label {
    display: flex;
    flex-direction: column;
    font-size: 9px;
    color: var(--text2);
    margin-bottom: 0;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
input[type="number"], input[type="text"], select {
    width: 100%; background: var(--surface);
    border: 2px solid var(--border); border-radius: 8px;
    padding: 6px 7px; color: var(--text);
    font-size: 16px; margin-top: 2px; outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
    font-family: inherit; -webkit-appearance: none;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
input:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.input-prefix, .input-suffix { display: flex; align-items: center; margin-top: 2px; }
.input-prefix span, .input-suffix span {
    background: var(--surface); border: 1px solid var(--border);
    padding: 6px 6px; font-size: 10px; color: var(--text2); font-weight: 700;
}
.input-prefix span { border-radius: 8px 0 0 8px; border-right: none; }
.input-prefix input { border-radius: 0 8px 8px 0; margin-top: 0; }
.input-suffix span { border-radius: 0 8px 8px 0; border-left: none; }
.input-suffix input { border-radius: 8px 0 0 8px; margin-top: 0; }

.input-wide { grid-column: 1 / -1; }

/* ====== BUTTONS ====== */
.btn-row {
    grid-column: 1 / -1;
    display: flex; gap: 4px; margin-top: 2px;
}
.calc-btn { display: none; }
.save-btn {
    flex: 1; padding: 8px 12px; background: var(--accent);
    color: #fff; border: none;
    border-radius: 8px; font-size: 11px; font-weight: 500;
    cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 3px;
    white-space: nowrap; font-family: inherit;
    -webkit-tap-highlight-color: transparent;
}
.save-btn:hover { background: var(--accent-dim); color: #fff; }
.save-btn.unsave-mode { background: var(--red); color: #fff; border: none; }
.save-btn.unsave-mode:hover { background: #dc2626; color: #fff; }
.save-btn svg { flex-shrink: 0; width: 12px; height: 12px; }

/* ====== RESULT CARD ====== */
.result-card {
    padding: 6px 8px;
    flex: 1; min-height: 0;
    display: flex; flex-direction: column;
    overflow: hidden;
    border-top: 2px solid var(--accent);
}

/* ====== HERO ====== */
.hero-result { text-align: center; padding: 4px 0 2px; flex-shrink: 0; }
.hero-label {
    display: block; font-size: 8px; color: var(--text2);
    text-transform: uppercase; letter-spacing: 0.5px;
}
.hero-value {
    display: block; font-size: 26px; font-weight: 800;
    color: var(--accent); letter-spacing: -0.5px; line-height: 1.15;
}
.hero-sub { display: block; font-size: 9px; color: var(--text2); margin-top: 1px; }

/* ====== STATS ====== */
.result-grid { display: flex; gap: 4px; margin: 4px 0; flex-shrink: 0; flex-wrap: wrap; }
.result-stat {
    flex: 1; text-align: center;
    background: var(--surface); border-radius: 4px; padding: 4px 2px;
    border: 1px solid var(--border); min-width: 60px;
}
.stat-value { display: block; font-size: 11px; font-weight: 700; color: var(--text); }
.stat-label { display: block; font-size: 7px; color: var(--text2); margin-top: 1px; text-transform: uppercase; }
.result-stat.gain .stat-value { color: var(--accent); }

/* ====== TIME TABS ====== */
.time-tabs { display: flex; gap: 3px; margin: 3px 0 1px; flex-shrink: 0; }
.time-tab {
    flex: 1; padding: 3px; text-align: center;
    font-size: 9px; font-weight: 600;
    color: var(--text2); background: var(--surface);
    border: 1px solid var(--border); border-radius: 3px;
    cursor: pointer; font-family: inherit;
    -webkit-tap-highlight-color: transparent;
}
.time-tab:hover { color: var(--text); }
.time-tab.active { color: var(--accent); border-color: var(--accent); background: rgba(124,58,237,0.05); }

/* ====== CHART ====== */
canvas {
    width: 100% !important;
    flex: 1;
    min-height: 80px;
    border-radius: 4px;
    background: var(--surface);
    border: 1px solid var(--border);
}

/* ====== RESULT ROWS ====== */
.result-row {
    display: flex; justify-content: space-between; align-items: center;
    padding: 4px 0; border-bottom: 1px solid var(--border);
    font-size: 10px; gap: 4px;
}
.result-row:last-of-type { border-bottom: none; }
.result-row.total { font-weight: 700; font-size: 11px; }
.gain { color: var(--accent); font-weight: 600; }
.loss { color: var(--red); font-weight: 600; }

/* ====== COMPARE ====== */
.compare-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin: 4px 0; flex-shrink: 0; }
.compare-col, .glass-inner {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 4px; padding: 6px;
}
.compare-col h4 { font-size: 10px; font-weight: 600; margin-bottom: 3px; color: var(--text); }

/* ====== BRACKET BARS ====== */
.bracket-bars { margin-top: 4px; flex: 1; min-height: 0; overflow-y: auto; }
.bracket-row {
    display: flex; align-items: center; gap: 6px;
    margin-bottom: 4px; font-size: 10px;
}
.bracket-label {
    width: 36px; text-align: right; color: var(--text2);
    font-weight: 600; flex-shrink: 0; font-size: 9px;
}
.bracket-bar-wrap {
    flex: 1; height: 18px; background: var(--surface);
    border-radius: 3px; overflow: hidden; position: relative;
    border: 1px solid var(--border);
}
.bracket-bar-fill {
    height: 100%; border-radius: 3px 0 0 3px;
    transition: width 0.6s ease;
    display: flex; align-items: center; padding-left: 4px;
    font-size: 8px; font-weight: 600; color: var(--white);
    white-space: nowrap; min-width: 0;
}
.bracket-bar-fill span { overflow: hidden; text-overflow: ellipsis; }
.bracket-tax {
    width: 54px; text-align: right; color: var(--text);
    font-weight: 600; flex-shrink: 0; font-size: 9px;
}

/* ====== CUSTOM RATE ====== */
.custom-rate-wrap { grid-column: 1 / -1; }

/* ====== BOTTOM NAV ====== */
.bottom-nav {
    display: flex; flex-shrink: 0;
    background: #1a1a1a; border-top: 1px solid var(--border);
    padding: 4px 0;
    padding-bottom: max(4px, env(safe-area-inset-bottom, 4px));
}
.bnav {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; gap: 2px;
    padding: 6px 0; background: none; border: none;
    color: #888; font-size: 9px; font-weight: 500;
    cursor: pointer; font-family: inherit;
    -webkit-tap-highlight-color: transparent;
    transition: color 0.15s;
}
.bnav svg { transition: transform 0.15s; }
.bnav:hover { color: #bbb; }
.bnav.active { color: var(--accent); }
.bnav.active svg { transform: scale(1.1); }

/* ══════════ DESKTOP ══════════ */
@media (min-width: 769px) {
    .sidebar { transform: translateX(0); box-shadow: none; }
    .sidebar-close { display: none; }
    .menu-btn { display: none; }
    .main { margin-left: var(--sidebar-w); }
    .topbar { height: 48px; padding: 0 20px; }
    .page-title { font-size: 16px; }
    .content { padding: 16px; max-width: 900px; margin: 0 auto; overflow-y: auto; }
    .ad-placeholder { height: 90px; line-height: 90px; }
    .ad-banner { padding: 5px 10px; }

    .calc-grid { flex-direction: row; gap: 16px; }
    .calc-grid .input-card { order: 0; }
    .calc-grid .result-card { order: 1; }
    .calc-grid .input-card { width: 320px; flex-shrink: 0; padding: 16px; }
    .calc-grid .result-card { flex: 1; min-width: 0; padding: 16px; }

    .input-card h3 { display: block; font-size: 11px; font-weight: 600; color: var(--text2); margin-bottom: 12px; }
    .input-fields { grid-template-columns: 1fr; gap: 0; }
    .input-card label { font-size: 11px; margin-bottom: 10px; }
    input[type="number"], input[type="text"], select { padding: 9px 10px; }
    .input-prefix span, .input-suffix span { padding: 9px 8px; font-size: 12px; }
    .save-btn { padding: 10px 14px; font-size: 12px; }
    .save-btn svg { width: 14px; height: 14px; }

    .hero-value { font-size: 42px; }
    .hero-label { font-size: 10px; }
    .hero-sub { font-size: 12px; }
    .hero-result { padding: 10px 0 6px; }
    .stat-value { font-size: 15px; }
    .stat-label { font-size: 9px; }
    .result-stat { padding: 7px 3px; }
    .result-grid { gap: 6px; margin: 8px 0; }
    .result-row { font-size: 13px; padding: 6px 0; }
    .result-row.total { font-size: 14px; }
    .time-tabs { margin: 6px 0 2px; }
    .time-tab { padding: 5px; font-size: 10px; }
    canvas { min-height: 180px; flex: unset; height: 200px; }
    .btn-row { margin-top: 6px; }
    .input-wide { grid-column: auto; }
    .bottom-nav { display: none; }

    .bracket-row { margin-bottom: 6px; font-size: 11px; }
    .bracket-label { width: 42px; font-size: 10px; }
    .bracket-bar-wrap { height: 22px; }
    .bracket-bar-fill { font-size: 9px; padding-left: 6px; }
    .bracket-tax { width: 62px; font-size: 10px; }
}

/* ====== SMALL MOBILE ====== */
@media (max-width: 380px) {
    .hero-value { font-size: 22px; }
    .stat-value { font-size: 10px; }
    .stat-label { font-size: 6px; }
    .ad-placeholder { height: 30px; line-height: 30px; font-size: 8px; }
    .ad-banner { padding: 2px 6px; }
    .topbar { height: 34px; }
    .page-title { font-size: 12px; }
}

/* ====== NUM POP ====== */
@keyframes numPop {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}
.hero-value.pop { animation: numPop 0.3s ease; }

/* ====== LIGHT MODE ====== */
body.light {
    --bg: #fff; --surface: #f5f5f0; --surface2: #eae8e0;
    --border: #e0ddd5; --text: #1a1a1a; --text2: #777;
    --text3: #aaa; --white: #fff;
}
body.light .ad-banner { background: #fafaf7; }
body.light .sidebar { background: #fff; box-shadow: 4px 0 20px rgba(0,0,0,0.08); }
body.light .topbar { background: #fff; }
body.light .bottom-nav { background: #fff; border-top: 1px solid #e0ddd5; }
body.light .bnav { color: #aaa; }
body.light .bnav.active { color: var(--accent); }
body.light input, body.light select { background: #f5f5f0; color: #1a1a1a; }

/* ====== THEME TOGGLE ====== */
.theme-toggle {
    margin-left: auto; background: none; border: none;
    color: var(--text2); cursor: pointer; padding: 4px;
    display: flex; align-items: center;
    -webkit-tap-highlight-color: transparent;
}
.theme-toggle:hover { color: var(--text); }

/* ====== ONBOARDING TOUR ====== */
.tour-overlay {
    position: fixed; inset: 0; z-index: 300;
    background: rgba(0,0,0,0.75);
    pointer-events: auto;
}
.tour-highlight {
    position: fixed; z-index: 301;
    box-shadow: 0 0 0 9999px rgba(0,0,0,0.85);
    border: 2px solid #7c3aed;
    border-radius: 10px;
    pointer-events: none;
    transition: all 0.3s ease;
    background: rgba(255,255,255,0.08);
}
.tour-tooltip {
    position: fixed; z-index: 302;
    background: #fff; border: none; color: #1a1a1a;
    border-radius: 12px; padding: 16px 18px;
    max-width: 300px; width: 90vw;
    box-shadow: 0 12px 40px rgba(0,0,0,0.5);
}
.tour-tooltip-text {
    font-size: 14px; color: #1a1a1a !important; line-height: 1.5;
    margin-bottom: 12px;
}
.tour-tooltip-footer {
    display: flex; align-items: center; justify-content: space-between;
}
.tour-step-count { font-size: 10px; color: #999 !important; }
.tour-btn-skip {
    background: none; border: none; color: #999 !important;
    font-size: 11px; cursor: pointer; font-family: inherit;
    padding: 4px 8px;
}
.tour-btn-skip:hover { color: #555 !important; }
.tour-btn-next {
    background: var(--accent); color: #fff; border: none;
    border-radius: 6px; padding: 6px 14px; font-size: 11px;
    font-weight: 600; cursor: pointer; font-family: inherit;
}
.tour-btn-next:hover { opacity: 0.9; }

.tour-replay-btn {
    width: 100%; padding: 8px; margin-top: 8px;
    background: none; border: 1px solid var(--border);
    border-radius: 8px; color: var(--text2);
    font-size: 11px; font-weight: 500;
    cursor: pointer; font-family: inherit;
    transition: all 0.15s;
}
.tour-replay-btn:hover { border-color: var(--accent); color: var(--accent); }

.guide-btn {
    margin-left: auto; padding: 4px 10px;
    background: var(--accent); color: #fff; border: none;
    border-radius: 6px; font-size: 10px; font-weight: 600;
    cursor: pointer; font-family: inherit;
    -webkit-tap-highlight-color: transparent;
    transition: opacity 0.15s;
    white-space: nowrap;
}
.guide-btn:hover { opacity: 0.85; }

::-webkit-scrollbar { width: 3px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
@supports (padding-bottom: env(safe-area-inset-bottom)) { body { padding-bottom: env(safe-area-inset-bottom); } }
