:root {
  --bg: #f2f2f7;
  --grouped: #ffffff;
  --ink: #000000;
  --label: #3c3c43;
  --secondary: rgba(60, 60, 67, 0.6);
  --separator: rgba(60, 60, 67, 0.12);
  --blue: #007aff;
  --blue-press: #0066d6;
  --green: #34c759;
  --orange: #ff9500;
  --red: #ff3b30;
  --gray: #8e8e93;
  --fill: rgba(120, 120, 128, 0.12);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  --radius: 12px;
  --radius-lg: 16px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Helvetica Neue", Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
}
html {
  -webkit-text-size-adjust: 100%;
}
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}
html.standalone body {
  background: var(--bg);
}
html.standalone .topbar {
  padding-top: calc(8px + env(safe-area-inset-top, 0px));
}

.app-shell {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: rgba(242, 242, 247, 0.86);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-right: 1px solid var(--separator);
  padding: 28px 14px 20px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand-block { padding: 0 10px 18px; }
.brand {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.03em;
}
.brand-sub {
  margin-top: 4px;
  color: var(--secondary);
  font-size: 13px;
}
.side-nav { display: grid; gap: 4px; }
.nav-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--ink);
  text-align: left;
  padding: 11px 12px;
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
}
.nav-btn:hover { background: var(--fill); }
.nav-btn.active {
  background: var(--blue);
  color: #fff;
  font-weight: 600;
}
.nav-btn.nav-accent:not(.active) {
  color: var(--blue);
  font-weight: 600;
}

.main { min-width: 0; padding-bottom: 88px; }
.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-end;
  padding: 18px 22px 10px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 5;
  background: rgba(242, 242, 247, 0.9);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 1px solid transparent;
}
.eyebrow {
  margin: 0 0 2px;
  color: var(--secondary);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.topbar h1 {
  margin: 0;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
}
.topbar-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.pill-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--grouped);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  color: var(--secondary);
  box-shadow: var(--shadow);
}
.pill-field input {
  border: 0;
  background: transparent;
  font: inherit;
  color: var(--ink);
  outline: none;
}

.content {
  padding: 8px 22px 40px;
  max-width: 1100px;
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 980px;
  padding: 8px 14px;
  background: var(--blue);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.btn:active { background: var(--blue-press); }
.btn.secondary {
  background: var(--fill);
  color: var(--blue);
}
.btn.block {
  width: 100%;
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 16px;
  margin-top: 8px;
}
.btn.danger {
  background: rgba(255, 59, 48, 0.12);
  color: var(--red);
}
.btn-row {
  display: grid;
  gap: 10px;
  margin-top: 8px;
}
@media (min-width: 640px) {
  .btn-row {
    grid-template-columns: 1fr 1fr;
  }
}
.ai-summary {
  margin: 0;
  padding: 12px 16px 16px;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.45;
  color: var(--label);
}

.grid-kpi {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}
.kpi {
  background: var(--grouped);
  border-radius: var(--radius-lg);
  padding: 14px 15px;
  box-shadow: var(--shadow);
}
.kpi .lbl {
  color: var(--secondary);
  font-size: 12px;
  font-weight: 500;
}
.kpi .val {
  margin-top: 6px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}
.kpi.primary {
  background: linear-gradient(160deg, #0a84ff, #007aff 55%, #0056d6);
  color: #fff;
}
.kpi.primary .lbl { color: rgba(255, 255, 255, 0.78); }

.group {
  background: var(--grouped);
  border-radius: var(--radius-lg);
  margin-bottom: 18px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.group-h {
  padding: 12px 16px 8px;
  color: var(--secondary);
  font-size: 13px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.group-h .right {
  float: right;
  text-transform: none;
  letter-spacing: 0;
  font-variant-numeric: tabular-nums;
}
.group-h .right .btn {
  padding: 6px 12px;
  font-size: 13px;
  margin: 0;
  border-radius: 8px;
}

.ios-list { list-style: none; margin: 0; padding: 0; }
.ios-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  padding: 14px 16px;
  border-top: 1px solid var(--separator);
  align-items: center;
  min-height: 52px;
}
.ios-row:first-child { border-top: 0; }
.ios-row .title { font-size: 16px; font-weight: 400; }
.ios-row .subtitle {
  margin-top: 2px;
  color: var(--secondary);
  font-size: 13px;
}
.ios-row .value {
  font-size: 16px;
  color: var(--secondary);
  font-variant-numeric: tabular-nums;
  text-align: right;
}
.expense-actions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}
.ios-row-tap {
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}
.ios-row-tap:active {
  background: rgba(0, 0, 0, 0.04);
}
.link-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--blue);
  font-size: 15px;
  font-weight: 600;
  padding: 8px 4px;
  cursor: pointer;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  pointer-events: none; /* row handles the tap */
}
.link-btn:active { opacity: 0.6; }
.ios-row.is-editing {
  background: rgba(0, 122, 255, 0.06);
}

.table-wrap { overflow: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
th, td {
  padding: 11px 14px;
  border-top: 1px solid var(--separator);
  text-align: left;
  white-space: nowrap;
}
th {
  font-size: 12px;
  color: var(--secondary);
  font-weight: 500;
  background: #fafafa;
}
td.num, th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-family: var(--mono);
  font-size: 13px;
}
tr.inactive { opacity: 0.45; }

.cell-input {
  width: 7.5rem;
  max-width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--separator);
  border-radius: 8px;
  font: inherit;
  font-variant-numeric: tabular-nums;
  text-align: right;
  background: #fff;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 980px;
  background: var(--fill);
  color: var(--label);
  font-size: 12px;
  font-weight: 600;
}
.badge.l1 { background: rgba(52, 199, 89, 0.15); color: #248a3d; }
.badge.l2 { background: rgba(255, 149, 0, 0.18); color: #9a5b00; }
.badge.l3 { background: rgba(255, 59, 48, 0.15); color: #d70015; }

.bars { padding: 4px 16px 14px; display: grid; gap: 10px; }
.bar-row {
  display: grid;
  grid-template-columns: 100px 1fr 88px;
  gap: 10px;
  align-items: center;
  font-size: 13px;
}
.bar-track {
  height: 8px;
  background: var(--fill);
  border-radius: 980px;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  background: var(--blue);
  border-radius: inherit;
}
.bar-row span:last-child {
  text-align: right;
  color: var(--secondary);
  font-variant-numeric: tabular-nums;
}

.form-grid {
  display: grid;
  gap: 0;
}
.field {
  display: grid;
  grid-template-columns: 110px 1fr;
  gap: 10px;
  align-items: center;
  padding: 10px 16px;
  border-top: 1px solid var(--separator);
  min-height: 44px;
}
.field:first-child { border-top: 0; }
.field label {
  color: var(--ink);
  font-size: 16px;
}
.field input,
.field select,
.field textarea {
  width: 100%;
  border: 0;
  outline: none;
  background: transparent;
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  text-align: right;
}
.field textarea {
  text-align: left;
  min-height: 64px;
  resize: vertical;
}
.hint {
  margin: 0 0 14px;
  padding: 12px 14px;
  border-radius: 12px;
  background: rgba(0, 122, 255, 0.08);
  color: var(--label);
  font-size: 14px;
  line-height: 1.45;
}
.hint code {
  font-family: var(--mono);
  font-size: 12px;
  background: rgba(0, 0, 0, 0.05);
  padding: 1px 5px;
  border-radius: 6px;
}
.segment {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--fill);
  border-radius: 10px;
  margin-bottom: 12px;
}
.segment button {
  flex: 1;
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 8px 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--secondary);
  cursor: pointer;
}
.segment button.active {
  background: #fff;
  color: var(--ink);
  box-shadow: var(--shadow);
}
.status {
  margin-top: 8px;
  font-size: 13px;
  color: var(--secondary);
  text-align: center;
}
.status.ok { color: #248a3d; }
.status.err { color: var(--red); }
.muted { color: var(--secondary); }
.error {
  padding: 16px;
  border-radius: 12px;
  background: rgba(255, 59, 48, 0.1);
  color: var(--red);
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 4px 16px 12px;
  align-items: flex-end;
}
.filter-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 120px;
  flex: 1;
}
.filter-field span {
  font-size: 12px;
  color: var(--secondary);
  font-weight: 500;
}
.filter-field select {
  appearance: none;
  border: 0;
  background: var(--fill);
  border-radius: 10px;
  padding: 10px 12px;
  font: inherit;
  font-size: 15px;
  color: var(--ink);
  min-height: 40px;
}

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .desktop-only { display: none !important; }
  .mobile-only { display: flex !important; }
  .tabbar { display: flex; }
  .main {
    padding-bottom: calc(72px + var(--safe-bottom));
    min-height: 100dvh;
  }
  .topbar {
    padding: 10px 14px 8px;
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
    align-items: center;
    gap: 10px;
  }
  .topbar h1 { font-size: 26px; }
  .topbar-title { min-width: 0; flex: 1; }
  .content { padding: 8px 12px 28px; }
  .grid-kpi {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }
  .kpi { padding: 12px; }
  .kpi .val { font-size: 20px; }
  .kpi.primary { grid-column: 1 / -1; }
  .kpi.primary .val { font-size: 28px; }
  .bar-row {
    grid-template-columns: 72px 1fr 72px;
    gap: 8px;
    font-size: 12px;
  }
  .segment {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .segment::-webkit-scrollbar { display: none; }
  .segment button {
    flex: 0 0 auto;
    min-width: 4.5rem;
    min-height: 40px;
    padding: 10px 14px;
    font-size: 14px;
  }
  .form-grid {
    grid-template-columns: 1fr;
  }
  .field {
    grid-template-columns: 1fr;
    gap: 6px;
    align-items: stretch;
    padding: 12px 16px;
    min-height: 0;
  }
  .field label {
    font-size: 13px;
    color: var(--secondary);
  }
  .field input,
  .field select {
    min-height: 44px;
    font-size: 16px;
    width: 100%;
  }
  .table-wrap {
    margin: 0 -4px;
  }
  table {
    font-size: 13px;
  }
  th, td {
    padding: 10px 10px;
  }
  .cell-input {
    width: 100%;
    min-width: 5.5rem;
    font-size: 16px; /* avoid iOS zoom */
  }
  .filter-bar {
    padding: 4px 12px 12px;
  }
  .filter-field {
    min-width: calc(50% - 8px);
  }
  .filter-field select,
  .form-grid input,
  .form-grid select,
  .pill-field input,
  .range-field input {
    font-size: 16px;
  }
  .btn.block {
    min-height: 48px;
    font-size: 16px;
  }
  .group-h .right .btn {
    min-height: 32px;
  }
}

.desktop-only { display: flex; }
.mobile-only { display: none !important; }

.icon-btn {
  appearance: none;
  border: 0;
  background: var(--fill);
  color: var(--blue);
  width: 40px;
  height: 40px;
  border-radius: 12px;
  font-size: 22px;
  line-height: 1;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}

.range-bar {
  display: none;
  gap: 8px;
  padding: 0 12px 10px;
}
.range-bar.mobile-only { display: none !important; }
.range-bar.range-hidden {
  display: none !important;
}
@media (max-width: 900px) {
  .range-bar.mobile-only:not(.range-hidden) {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
  }
}
.range-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--grouped);
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 11px;
  color: var(--secondary);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  box-shadow: var(--shadow);
}
.range-field input {
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 15px;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  color: var(--ink);
  outline: none;
  width: 100%;
  min-height: 28px;
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.32);
  z-index: 40;
}
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  background: #f2f2f7;
  border-radius: 16px 16px 0 0;
  padding: 8px 12px calc(16px + var(--safe-bottom));
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.12);
  max-height: min(80vh, 560px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.sheet[hidden],
.sheet-backdrop[hidden] {
  display: none !important;
}
.sheet-handle {
  width: 36px;
  height: 5px;
  border-radius: 999px;
  background: rgba(60, 60, 67, 0.28);
  margin: 4px auto 10px;
}
.sheet-note {
  margin: 0 4px 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(0, 122, 255, 0.08);
  color: var(--label);
  font-size: 13px;
  line-height: 1.35;
}
.icon-btn.has-dot,
.tab.has-dot {
  position: relative;
}
.icon-btn.has-dot::after,
.tab.has-dot::after {
  content: "";
  position: absolute;
  top: 6px;
  right: 8px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--orange);
  box-shadow: 0 0 0 2px rgba(249, 249, 249, 0.95);
}
.tab.has-dot::after {
  top: 4px;
  right: calc(50% - 18px);
}
.sheet-list {
  background: var(--grouped);
  border-radius: 14px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.sheet-item {
  appearance: none;
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--separator);
  background: transparent;
  text-align: left;
  padding: 16px;
  font-size: 17px;
  color: var(--blue);
  cursor: pointer;
  min-height: 52px;
}
.sheet-item:last-child { border-bottom: 0; }
.sheet-item.danger { color: var(--red); }
.sheet-item:active { background: var(--fill); }

.tabbar {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 6px 4px calc(6px + var(--safe-bottom));
  background: rgba(249, 249, 249, 0.94);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--separator);
  z-index: 20;
}
.tab {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--gray);
  font-size: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 2px;
  cursor: pointer;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}
.tab span { font-size: 20px; line-height: 1; }
.tab.active { color: var(--blue); }

/* Mobile nav must win over earlier base `display:none` rules */
@media (max-width: 900px) {
  .desktop-only { display: none !important; }
  .mobile-only { display: flex !important; }
  .tabbar { display: flex !important; }
}

body.sheet-open {
  overflow: hidden;
}

.install-tip {
  display: none;
  margin: 0 0 12px;
  padding: 14px 16px;
  border-radius: 14px;
  background: var(--grouped);
  box-shadow: var(--shadow);
  font-size: 14px;
  line-height: 1.4;
  color: var(--label);
}
.install-tip strong {
  color: var(--ink);
  display: block;
  margin-bottom: 4px;
  font-size: 15px;
}
@media (max-width: 900px) {
  html:not(.standalone) .install-tip {
    display: block;
  }
}

