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

:root {
  --bg: #0d0d0d;
  --bg2: #141414;
  --bg3: #1a1a1a;
  --ink: #f0ece3;
  --ink2: #88887f;
  --ink3: #55554e;
  --accent: #c9a227;
  --accent2: #e0c050;
  --accent3: #e8c84a;
  --accent4: #b89030;
  --border: rgba(255,255,255,0.1);
  --border2: rgba(255,255,255,0.07);
  --gold-grad: linear-gradient(120deg, #a07820 0%, #f0d060 35%, #c9a227 65%, #f5e090 100%);
  --radius: 4px;
  --radius2: 8px;
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: 'DM Sans', sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  min-height: 100dvh; /* avoids mobile URL-bar jump where supported */
  font-weight: 300;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

/* ── NAV ── */
nav {
  position: sticky; top: 0; z-index: 100;
  background: #000;
  display: flex; align-items: stretch;
  padding: 0 2rem;
  /* Respect notch / status bar when installed as a standalone app */
  padding-left: max(2rem, env(safe-area-inset-left));
  padding-right: max(2rem, env(safe-area-inset-right));
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid rgba(255,255,255,0.07);
}
.nav-brand {
  font-family: 'DM Serif Display', serif;
  color: var(--ink);
  font-size: 1.1rem;
  letter-spacing: 0.02em;
  padding: 1rem 0;
  margin-right: 2.5rem;
  white-space: nowrap;
  display: flex; align-items: center; gap: 0.5rem;
}
.brand-logo { height: 50px; width: auto; display: block; }
.nav-brand span {
  font-style: italic;
  background: var(--gold-grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.nav-tabs { display: flex; gap: 0; flex: 1; overflow-x: auto; }
.nav-tab {
  padding: 0 1.25rem;
  font-size: 0.78rem;
  font-weight: 400;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(240,236,227,0.4);
  cursor: pointer;
  border: none; background: none;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
  white-space: nowrap;
  display: flex; align-items: center; gap: 0.4rem;
}
.nav-tab:hover { color: rgba(240,236,227,0.75); }
.nav-tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.nav-tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.nav-tab .dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; opacity: 0.5;
}
.nav-tab[aria-selected="true"] .dot { opacity: 1; }

/* ── LAYOUT ── */
main {
  max-width: 960px; margin: 0 auto; padding: 2.5rem 1.5rem 4rem;
  padding-left: max(1.5rem, env(safe-area-inset-left));
  padding-right: max(1.5rem, env(safe-area-inset-right));
  padding-bottom: calc(4rem + env(safe-area-inset-bottom));
}

.calc-panel { display: none; animation: fadeIn 0.2s ease; }
.calc-panel.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.panel-header { margin-bottom: 2rem; }
.panel-header h1 {
  font-family: 'DM Serif Display', serif;
  font-size: 2.2rem;
  font-weight: 400;
  line-height: 1.15;
  letter-spacing: -0.01em;
  margin-bottom: 0.4rem;
}
.panel-header h1 em {
  font-style: italic;
  background: var(--gold-grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.panel-header p { font-size: 0.9rem; color: var(--ink2); max-width: 480px; line-height: 1.6; }

/* ── CONTROLS ── */
.controls-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem 2rem; margin-bottom: 1.75rem; }
@media (max-width: 600px) { .controls-grid { grid-template-columns: 1fr; } }

.control-row { display: flex; flex-direction: column; gap: 6px; }

/* ── INFO TOOLTIPS ── */
.info-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 14px; height: 14px; border-radius: 50%;
  border: 1px solid var(--ink3); color: var(--ink3);
  font-size: 8px; font-family: 'DM Sans', sans-serif; font-weight: 500;
  cursor: help; position: relative; margin-left: 5px;
  vertical-align: middle; flex-shrink: 0; line-height: 1;
  background: transparent;
}
.info-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.info-btn::after {
  content: attr(data-tip);
  position: absolute; bottom: calc(100% + 7px); left: 50%;
  transform: translateX(-50%);
  background: #222; color: var(--ink);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 6px; padding: 7px 10px;
  font-size: 0.7rem; line-height: 1.5;
  white-space: normal; width: 190px;
  pointer-events: none; opacity: 0;
  transition: opacity 0.15s; z-index: 200;
  font-weight: 300; text-transform: none; letter-spacing: 0;
}
.info-btn:hover::after,
.info-btn:focus::after,
.info-btn:focus-visible::after,
.info-btn.open::after { opacity: 1; }

.control-row label {
  font-size: 0.72rem; font-weight: 500;
  letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--ink3);
}
.control-row .ctrl-val {
  font-family: 'DM Mono', monospace;
  font-size: 1.05rem; font-weight: 500;
  color: var(--ink);
  min-height: 1.4rem;
}

/* ── SLIDER + NUMBER INPUT ── */
.slider-wrap { display: flex; align-items: center; gap: 10px; }
.slider-wrap input[type=range] { flex: 1; }
.num-input {
  width: 95px;
  font-family: 'DM Mono', monospace;
  font-size: 0.85rem;
  background: var(--bg2);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 6px;
  text-align: right;
  font-weight: 400;
  -moz-appearance: textfield;
}
.num-input::-webkit-outer-spin-button,
.num-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.num-input:focus { outline: none; border-color: var(--accent); color: var(--ink); }

input[type=range] {
  -webkit-appearance: none; width: 100%; height: 2px;
  background: rgba(255,255,255,0.12);
  outline: none; cursor: pointer;
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); cursor: pointer;
  transition: transform 0.1s;
}
input[type=range]:hover::-webkit-slider-thumb { transform: scale(1.2); }
input[type=range]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px rgba(201,162,39,0.3); }
input[type=range]::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); cursor: pointer; border: none;
}

/* ── STATS ── */
.stats-row { display: grid; gap: 10px; margin-bottom: 1.75rem; }
.stats-row.cols-3 { grid-template-columns: repeat(3, 1fr); }
.stats-row.cols-4 { grid-template-columns: repeat(4, 1fr); }
.stats-row.cols-5 { grid-template-columns: repeat(5, 1fr); }
@media (max-width: 600px) {
  .stats-row.cols-3, .stats-row.cols-4, .stats-row.cols-5 { grid-template-columns: repeat(2, 1fr); }
}
.stat-card {
  background: var(--bg3);
  border: 1px solid var(--border2);
  border-radius: var(--radius2);
  padding: 14px 16px;
}
.stat-card .sc-label {
  font-size: 0.68rem; font-weight: 500;
  letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--ink3); margin-bottom: 5px;
}
.stat-card .sc-val {
  font-family: 'DM Mono', monospace;
  font-size: 1.15rem; font-weight: 500;
  color: var(--ink);
}
.stat-card .sc-val.accent { color: var(--accent); }
.stat-card .sc-val.green { color: var(--accent3); }
.stat-card .sc-val.blue { color: var(--accent2); }
.stat-card .sc-val.purple { color: var(--accent4); }

/* ── CHART ── */
.chart-section { background: var(--bg3); border: 1px solid var(--border2); border-radius: var(--radius2); padding: 1.25rem; }
.chart-legend {
  display: flex; flex-wrap: wrap; gap: 14px;
  margin-bottom: 14px; font-size: 0.75rem; color: var(--ink2);
}
.chart-legend span { display: flex; align-items: center; gap: 5px; }
.chart-legend .swatch { width: 10px; height: 10px; border-radius: 2px; flex-shrink: 0; }
.chart-wrap { position: relative; width: 100%; }

/* ── ACTION BAR ── */
.action-bar {
  display: flex; justify-content: flex-end; gap: 8px;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}
.action-btn {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 14px; font-size: 0.75rem; font-weight: 400;
  letter-spacing: 0.05em; border: 1px solid var(--border);
  border-radius: var(--radius); background: transparent;
  cursor: pointer; color: var(--ink2); transition: all 0.12s;
  font-family: 'DM Sans', sans-serif;
}
.action-btn:hover { color: var(--ink); border-color: var(--accent); }
.action-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.action-btn.success { color: var(--accent); border-color: var(--accent); }

/* ── PRINT ── */
@media print {
  nav, .action-bar, .inner-tabs, #widget-section, .info-btn, .num-input, .add-debt-row, .debt-row .remove-btn { display: none !important; }
  body { background: #fff !important; color: #111 !important; }
  .calc-panel { display: none !important; }
  .calc-panel.active { display: block !important; }
  .stat-card { background: #f5f5f5 !important; border: 1px solid #ddd !important; }
  .sc-val, .ctrl-val { color: #111 !important; }
  .sc-label, .control-row label { color: #666 !important; }
  input[type=range] { display: none !important; }
  .chart-section { background: #fff !important; border: 1px solid #ddd !important; }
  .panel-header h1 em { -webkit-text-fill-color: #c9a227 !important; }
}

/* ── TABS (inner) ── */
.inner-tabs { display: flex; gap: 4px; margin-bottom: 1rem; flex-wrap: wrap; }
.inner-tab {
  padding: 5px 14px;
  font-size: 0.75rem; font-weight: 400;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent; cursor: pointer;
  color: var(--ink2); transition: all 0.12s;
  font-family: inherit;
}
.inner-tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.inner-tab.active { background: var(--accent); color: #0d0d0d; border-color: var(--accent); font-weight: 500; }

/* ── VERDICT ── */
.verdict {
  padding: 1rem 1.25rem;
  border-radius: var(--radius2);
  border-left: 3px solid var(--accent);
  background: var(--bg2);
  margin-bottom: 1.5rem;
  font-size: 0.875rem;
  line-height: 1.6;
}
.verdict strong { font-weight: 500; }

/* ── DIVIDER ── */
.section-divider {
  border: none; border-top: 1px solid var(--border2);
  margin: 1.5rem 0;
}

/* ── DEBT LIST ── */
.debt-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 1rem; }
.debt-row {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr 1fr auto;
  gap: 8px;
  align-items: center;
  background: var(--bg2);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  padding: 10px 12px;
}
.debt-row input[type=text], .debt-row input[type=number] {
  width: 100%;
  background: var(--bg3);
  color: var(--ink);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  padding: 6px 8px;
  font-family: 'DM Mono', monospace;
  font-size: 0.82rem;
  -moz-appearance: textfield;
}
.debt-row input[type=text] { font-family: 'DM Sans', sans-serif; }
.debt-row input::-webkit-outer-spin-button,
.debt-row input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.debt-row input:focus { outline: none; border-color: var(--accent); }
.debt-row .field-label {
  display: block;
  font-size: 0.6rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink3);
  margin-bottom: 3px;
}
.remove-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--ink3);
  border-radius: var(--radius);
  width: 28px; height: 28px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; line-height: 1;
  transition: all 0.1s;
}
.remove-btn:hover { color: #e88; border-color: #e88; }
.add-debt-row {
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--ink2);
  padding: 8px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.8rem;
  transition: all 0.1s;
}
.add-debt-row:hover { color: var(--accent); border-color: var(--accent); }
@media (max-width: 600px) {
  .debt-row { grid-template-columns: 1fr 1fr auto; }
  .debt-row .field-name { grid-column: 1 / -1; }
}

/* ── FOOTER ── */
.footer {
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border2);
  font-size: 0.7rem;
  color: var(--ink3);
  text-align: center;
}
.footer a { color: var(--ink2); text-decoration: none; }
.footer a:hover { color: var(--accent); }

/* ════════════════════════════════════════════════
   MOBILE OPTIMIZATION
   ════════════════════════════════════════════════ */

/* Touch devices (any size): bigger hit targets, no zoom-on-focus.
   iOS zooms when an input's font-size is < 16px, so force 16px. */
@media (pointer: coarse) {
  .num-input,
  .debt-row input[type=text],
  .debt-row input[type=number] { font-size: 16px; }

  /* Thin visual track, tall invisible touch area, large thumb */
  input[type=range] { height: 26px; background: transparent; }
  input[type=range]::-webkit-slider-runnable-track {
    height: 3px; border-radius: 3px; background: rgba(255,255,255,0.16);
  }
  input[type=range]::-webkit-slider-thumb { width: 24px; height: 24px; margin-top: -10.5px; }
  input[type=range]::-moz-range-track { height: 3px; border-radius: 3px; background: rgba(255,255,255,0.16); }
  input[type=range]::-moz-range-thumb { width: 24px; height: 24px; }

  .info-btn { width: 18px; height: 18px; font-size: 10px; }
  .action-btn { min-height: 40px; padding: 8px 16px; }
  .inner-tab { min-height: 38px; }
  .remove-btn { width: 36px; height: 36px; font-size: 20px; }
  .nav-tab { min-height: 48px; }
}

/* Phone widths */
@media (max-width: 600px) {
  nav { padding-left: max(1rem, env(safe-area-inset-left)); padding-right: max(1rem, env(safe-area-inset-right)); }
  .nav-brand { margin-right: 1.25rem; }
  .brand-logo { height: 38px; }
  .nav-tabs { -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .nav-tabs::-webkit-scrollbar { display: none; }
  .nav-tab { padding: 0 0.85rem; font-size: 0.72rem; letter-spacing: 0.05em; }

  main { padding: 1.5rem 1rem 3rem; padding-bottom: calc(3rem + env(safe-area-inset-bottom)); }

  .panel-header { margin-bottom: 1.5rem; }
  .panel-header h1 { font-size: 1.7rem; }
  .panel-header p { font-size: 0.85rem; }

  .controls-grid { gap: 1.1rem 1rem; margin-bottom: 1.5rem; }
  .num-input { width: 84px; }

  .stat-card { padding: 12px 13px; }
  .stat-card .sc-val { font-size: 1.05rem; }

  /* Keep tooltips inside the viewport on small screens */
  .info-btn::after { width: min(190px, 72vw); }

  .verdict { font-size: 0.84rem; padding: 0.9rem 1rem; }
  .chart-section { padding: 1rem 0.85rem; }

  .action-bar { justify-content: stretch; gap: 6px; }
  .action-bar .action-btn { flex: 1 1 auto; justify-content: center; }
}

/* Very narrow phones */
@media (max-width: 380px) {
  .stats-row.cols-3, .stats-row.cols-4, .stats-row.cols-5 { grid-template-columns: 1fr; }
  .panel-header h1 { font-size: 1.5rem; }
}
