:root {
  color-scheme: light dark;
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #e9e9ee;
  --text: #000000;
  --text-secondary: rgba(60, 60, 67, 0.6);
  --separator: rgba(60, 60, 67, 0.29);
  --accent: #007aff;
  --accent-soft: rgba(0, 122, 255, 0.12);
  --green: #34c759;
  --green-soft: rgba(52, 199, 89, 0.14);
  --red: #ff3b30;
  --red-soft: rgba(255, 59, 48, 0.12);
  --orange: #ff9500;
  --orange-soft: rgba(255, 149, 0, 0.14);
  --purple: #af52de;
  --purple-soft: rgba(175, 82, 222, 0.14);
  --blur-bg: rgba(242, 242, 247, 0.78);
  --radius-lg: 18px;
  --radius-md: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-2: #2c2c2e;
    --text: #ffffff;
    --text-secondary: rgba(235, 235, 245, 0.6);
    --separator: rgba(84, 84, 88, 0.65);
    --accent: #0a84ff;
    --accent-soft: rgba(10, 132, 255, 0.18);
    --green: #30d158;
    --green-soft: rgba(48, 209, 88, 0.18);
    --red: #ff453a;
    --red-soft: rgba(255, 69, 58, 0.18);
    --orange: #ff9f0a;
    --orange-soft: rgba(255, 159, 10, 0.18);
    --purple: #bf5af2;
    --purple-soft: rgba(191, 90, 242, 0.18);
    --blur-bg: rgba(0, 0, 0, 0.72);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.7rem 1.1rem;
  position: sticky;
  top: 0;
  background: var(--blur-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  z-index: 20;
  border-bottom: 0.5px solid var(--separator);
}

.topbar-title { font-size: 1.05rem; font-weight: 600; letter-spacing: -0.01em; }
.topbar-actions { display: flex; gap: 1rem; }

.btn-text {
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  padding: 0.2rem 0.1rem;
}
.btn-text:active { opacity: 0.5; }
.file-btn { display: inline-flex; align-items: center; }

/* ---------- Desktop segmented control ---------- */
.segmented {
  display: none;
  max-width: 720px;
  margin: 0.9rem auto 0;
  padding: 3px;
  background: var(--surface-2);
  border-radius: 11px;
  gap: 2px;
}

.seg-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.45rem 0.5rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s ease, box-shadow 0.2s ease;
}

.seg-btn.active {
  background: var(--surface);
  box-shadow: var(--shadow);
}

@media (min-width: 768px) {
  .segmented { display: flex; }
}

/* ---------- Main content ---------- */
main {
  max-width: 720px;
  margin: 0 auto;
  padding: 0.5rem 1.1rem 6.5rem;
}

@media (min-width: 768px) {
  main { padding-bottom: 3rem; }
}

.view { display: none; }
.view.active { display: block; animation: fadeIn 0.25s ease; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.large-title {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0.6rem 0 1rem;
}

.section-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-secondary);
  margin: 0 0 0.5rem 0.9rem;
}

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-top: 1.5rem;
}
.list-header .section-title { margin-top: 0; }
.list-header-controls { display: flex; gap: 0.5rem; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- Stat cards ---------- */
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 0.65rem;
  margin-bottom: 1.2rem;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 0.9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  box-shadow: var(--shadow);
}

.stat-label { font-size: 0.78rem; color: var(--text-secondary); font-weight: 500; }
.stat-value { font-size: 1.25rem; font-weight: 700; letter-spacing: -0.01em; }

/* ---------- Generic iOS card ---------- */
.ios-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 1rem;
  margin-bottom: 1.2rem;
  box-shadow: var(--shadow);
}

.chart-card .section-title { margin-left: 0; }
.chart-wrap { position: relative; height: 240px; }

/* ---------- Grouped list (iOS Settings style) ---------- */
.ios-list {
  background: var(--surface);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow);
  margin-bottom: 0.9rem;
}

.ios-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.7rem 1rem;
  border-bottom: 0.5px solid var(--separator);
}
.ios-row:last-child { border-bottom: none; }

.ios-row label {
  flex: 0 0 130px;
  font-size: 0.92rem;
  color: var(--text);
}

.ios-row input,
.ios-row select {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.95rem;
  text-align: right;
  padding: 0.15rem 0;
  font-family: inherit;
  min-width: 0;
}
.ios-row input:focus,
.ios-row select:focus { outline: none; }
.ios-row input::placeholder { color: var(--text-secondary); }

/* Native dropdown popups aren't restyled by the OS, but the closed control
   and its <option> list DO honor explicit color/background — without this,
   transparent selects inherit light text that vanishes on the popup's
   default white background. */
select {
  color-scheme: light dark;
}
select option {
  background-color: var(--surface);
  color: var(--text);
}

/* Entry rows rendered inside .ios-list (spend log) */
.entry-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 0.5px solid var(--separator);
}
.entry-row:last-child { border-bottom: none; }

.entry-main { display: flex; flex-direction: column; gap: 0.25rem; min-width: 0; }
.entry-title { font-size: 0.98rem; font-weight: 600; }
.entry-sub { font-size: 0.82rem; color: var(--text-secondary); }
.entry-tags { display: flex; flex-wrap: wrap; gap: 0.3rem; margin-top: 0.1rem; }

.entry-side { display: flex; flex-direction: column; align-items: flex-end; gap: 0.35rem; flex-shrink: 0; }
.entry-amount { font-size: 1rem; font-weight: 700; white-space: nowrap; }
.entry-amount-sub { font-size: 0.75rem; color: var(--text-secondary); white-space: nowrap; }

.row-actions { display: flex; gap: 0.6rem; }
.icon-btn {
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  padding: 0.1rem 0.2rem;
}
.icon-btn.danger { color: var(--red); }
.icon-btn:active { opacity: 0.5; }

/* ---------- Forms ---------- */
.ios-form { margin-bottom: 0.5rem; }

.ios-textarea {
  width: 100%;
  border: none;
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-lg);
  padding: 0.9rem 1rem;
  font-family: inherit;
  font-size: 0.95rem;
  resize: vertical;
  min-height: 130px;
  margin-bottom: 0.9rem;
  box-shadow: var(--shadow);
}
.ios-textarea:focus { outline: none; }
.ios-textarea::placeholder { color: var(--text-secondary); }

.btn-primary {
  display: block;
  width: 100%;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 1rem;
  font-weight: 600;
  padding: 0.8rem;
  border-radius: 14px;
  cursor: pointer;
  margin-bottom: 1.4rem;
  transition: transform 0.1s ease, opacity 0.1s ease;
}
.btn-primary:active { transform: scale(0.98); opacity: 0.85; }

.form-actions { display: flex; gap: 0.6rem; margin-bottom: 1.4rem; }
.form-actions .btn-primary { margin-bottom: 0; flex: 1; }

.btn-secondary {
  flex: 0 0 auto;
  border: none;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 0.8rem 1.1rem;
  border-radius: 14px;
  cursor: pointer;
}
.btn-secondary:active { opacity: 0.6; }

/* ---------- Collapsible "advanced fields" disclosure ---------- */
.ios-details {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  margin-bottom: 0.9rem;
  overflow: hidden;
}

.ios-details summary {
  padding: 0.75rem 1rem;
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--accent);
  cursor: pointer;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.ios-details summary::-webkit-details-marker { display: none; }
.ios-details summary::after {
  content: '⌄';
  color: var(--text-secondary);
  transition: transform 0.2s ease;
}
.ios-details[open] summary { border-bottom: 0.5px solid var(--separator); }
.ios-details[open] summary::after { transform: rotate(180deg); }
.ios-details .ios-list { box-shadow: none; border-radius: 0; margin-bottom: 0; }

.mini-select, .mini-search {
  border: none;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.82rem;
  padding: 0.35rem 0.6rem;
  border-radius: 999px;
  font-family: inherit;
}
.mini-select:focus, .mini-search:focus { outline: none; }

/* ---------- Card list (prompts / links / portfolio) ---------- */
.card-list { display: flex; flex-direction: column; gap: 0.7rem; }

.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 0.95rem 1.05rem;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.6rem;
}

.card h3 { margin: 0 0 0.3rem; font-size: 1rem; font-weight: 600; }
.card h3 a { color: var(--text); text-decoration: none; }
.card h3 a:active { opacity: 0.6; }

.tags { margin: 0.4rem 0; display: flex; flex-wrap: wrap; gap: 0.3rem; }
.muted { color: var(--text-secondary); font-size: 0.85rem; margin: 0.4rem 0 0; }

/* ---------- Badges (iOS capsule tags) ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 0.18rem 0.6rem;
  font-size: 0.72rem;
  font-weight: 600;
}
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-orange { background: var(--orange-soft); color: var(--orange); }
.badge-purple { background: var(--purple-soft); color: var(--purple); }
.badge-red { background: var(--red-soft); color: var(--red); }
.badge-gray { background: var(--surface-2); color: var(--text-secondary); }

.prompt-body {
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--surface-2);
  border-radius: var(--radius-md);
  padding: 0.75rem;
  max-height: 260px;
  overflow-y: auto;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 0.82rem;
  margin: 0.5rem 0 0;
}

.empty { color: var(--text-secondary); text-align: center; padding: 1.4rem 0; font-size: 0.9rem; }

/* ---------- Mobile bottom tab bar ---------- */
.tabbar {
  display: flex;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--blur-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--separator);
  padding: 0.35rem 0.2rem calc(0.35rem + env(safe-area-inset-bottom));
  z-index: 20;
}

.tabbar-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.68rem;
  font-weight: 500;
  padding: 0.2rem 0;
  cursor: pointer;
}

.tabbar-icon { font-size: 1.2rem; line-height: 1; }
.tabbar-btn.active { color: var(--accent); }
.tabbar-btn:active { opacity: 0.6; }

@media (min-width: 768px) {
  .tabbar { display: none; }
}

/* ---------- Small screens: tighten spacing ---------- */
@media (max-width: 480px) {
  .large-title { font-size: 1.7rem; }
  main { padding-left: 0.85rem; padding-right: 0.85rem; }
}
