:root {
  color-scheme: light;
  --bg: #edf3ef;
  --panel: #ffffff;
  --ink: #1e2d27;
  --muted: #6b8078;
  --line: #d4e2db;
  --accent: #2e7d5e;
  --accent-strong: #1d5e44;
  --danger: #b03a4a;
  --soft: #f3f8f5;
  --shadow: 0 16px 40px rgba(20, 42, 32, 0.08);
  --chart-line: #5ec49a;
  --chart-card: #1e3d30;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

button {
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  min-height: 42px;
  padding: 0 16px;
  font-weight: 700;
  cursor: pointer;
}

button:hover {
  background: var(--accent-strong);
}

button.secondary,
button.icon-button {
  background: var(--line);
  color: var(--ink);
}

button.secondary:hover,
button.icon-button:hover {
  background: var(--muted);
  color: var(--panel);
}

button.icon-button {
  width: 42px;
  padding: 0;
  font-size: 20px;
}

button.export-btn {
  background: var(--line);
  color: var(--accent-strong);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.5px;
  min-height: 42px;
  padding: 0 12px;
  border-radius: 8px;
}
button.export-btn:hover { background: var(--muted); color: var(--panel); }

.icon-button svg {
  width: 21px;
  height: 21px;
  fill: currentColor;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 11px 12px;
}

textarea {
  resize: vertical;
}

.app-shell {
  width: min(1080px, 100%);
  margin: 0 auto;
  height: 100dvh;
  padding: 14px 14px 0;
  overflow: hidden;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
  /* Never let a panel break out of its container */
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
  overflow-x: hidden;
}

.auth-layout {
  min-height: 90vh;
  display: grid;
  place-items: center;
}

.auth-panel,
.setup-panel {
  width: min(440px, 100%);
}

.auth-panel {
  display: grid;
  gap: 14px;
}

.stack {
  display: grid;
  gap: 14px;
}

.button-row,
.top-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  min-height: 62px;
  margin-bottom: 12px;
}

.topbar h1,
.panel h1,
.panel h2 {
  margin: 0;
}

.kicker {
  margin: 0 0 4px;
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.muted,
.form-note {
  margin: 0;
  color: var(--muted);
}

.app-message {
  border: 1px solid #f0c7ce;
  background: #fff4f5;
  color: var(--danger);
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 14px;
  font-weight: 700;
}

#appView {
  height: 100%;
  overflow: hidden;
}

.app-frame {
  display: grid;
  height: calc(100% - 142px);
  min-height: 0;
  padding-bottom: 12px;
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 10px;
}

.summary-grid.compact {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.summary-card {
  min-height: 74px;
  display: grid;
  gap: 7px;
  align-content: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 16px;
}

.summary-card.primary {
  background: var(--chart-card);
  border-color: var(--chart-card);
  color: #fff;
}

.summary-card span {
  color: inherit;
  opacity: 0.78;
  font-size: 14px;
  font-weight: 700;
}

.summary-card strong {
  font-size: 22px;
  line-height: 1;
}

.segmented-control {
  display: flex;
  gap: 6px;
  align-items: center;
}

.segmented-control button {
  background: transparent;
  color: var(--muted);
  border-radius: 0;
}

.segmented-control button.active {
  color: var(--accent-strong);
  box-shadow: inset 0 -3px 0 var(--accent);
}

.segmented-control {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px;
}

.settings-tabs {
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.segmented-control button {
  border-radius: 6px;
  box-shadow: none;
  min-height: 36px;
}

.segmented-control button.active {
  background: var(--accent);
  color: #fff;
  box-shadow: none;
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: grid;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}

.dashboard-scroll,
.list-screen,
.workspace-grid {
  height: 100%;
  min-height: 0;
  overflow: auto;
  padding: 2px 2px 16px;
  -webkit-overflow-scrolling: touch;
}

.hero-balance {
  border-radius: 8px;
  padding: 20px 24px;
  margin-bottom: 12px;
  color: #fff;
  background: linear-gradient(135deg, var(--chart-card) 0%, var(--accent) 54%, #d89c45 100%);
  box-shadow: var(--shadow);
}

.hero-dual {
  display: grid;
  grid-template-columns: 1fr 1px 1fr;
  align-items: stretch;
  /* 14px gap on each side of the divider — evenly splits the space */
  column-gap: 14px;
}

.hero-dual-item {
  display: grid;
  gap: 6px;
  padding: 4px 0;
  min-width: 0;
  overflow: hidden;
}

/* No extra padding needed — column-gap handles spacing */
.hero-dual-item:last-child {
  padding-left: 0;
}

.hero-dual-divider {
  background: rgba(255, 255, 255, 0.25);
  /* width comes from the 1px grid column; no margins needed */
}

.hero-dual-item > span {
  opacity: 0.75;
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.hero-balance strong {
  /* 390px → ~27px · 430px → ~30px · desktop → 36px */
  font-size: clamp(22px, 7vw, 36px);
  line-height: 1.1;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
}

.hero-sub-row {
  font-size: 12px;
  opacity: 0.7;
}

.hero-sub-row b {
  margin-left: 3px;
}

.screen-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.screen-heading h2 {
  margin: 0;
}

.compact-button {
  min-height: 34px;
  padding: 0 12px;
}

.workspace-grid {
  display: grid;
  grid-template-columns: minmax(280px, 390px) 1fr;
  gap: 16px;
  align-items: start;
}

.workspace-grid > .panel {
  max-height: none;
  min-height: 0;
  overflow: visible;
}

.section-heading {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: start;
  margin-bottom: 14px;
}

.record-list,
.account-list,
.pill-list {
  display: grid;
  gap: 10px;
}

.record,
.account-row {
  display: grid;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  padding: 12px;
}

.record-main,
.account-main {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.record-title,
.account-title {
  font-weight: 800;
}

.record-amount,
.account-balance {
  font-weight: 900;
  white-space: nowrap;
}

.positive {
  color: #13704f;
}

.negative {
  color: var(--danger);
}

.meta-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 13px;
}

.subform {
  display: grid;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  padding: 12px;
}

.calculator-pad {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f4f0ea;
  padding: 10px;
}

.calculator-pad button {
  min-height: 44px;
  border: 1px solid #d8cec0;
  background: #fffdf9;
  color: var(--ink);
  font-size: 18px;
  font-weight: 900;
}

.calculator-pad button:hover {
  background: #f2e6d4;
}

.calculator-pad button[data-calc-value="/"],
.calculator-pad button[data-calc-value="*"],
.calculator-pad button[data-calc-value="-"],
.calculator-pad button[data-calc-value="+"],
.calculator-pad .equals {
  background: var(--chart-card);
  border-color: var(--chart-card);
  color: #fff;
}

.calculator-pad button[data-calc-action="clear"],
.calculator-pad button[data-calc-action="backspace"] {
  background: var(--soft);
}

.calculator-pad .zero {
  grid-column: span 1;
}

.repay-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
}

.repay-row button {
  min-height: 40px;
}

.button-pair {
  display: inline-flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.chart-panel {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.mini-chart {
  display: grid;
  gap: 10px;
}

.mini-chart h3 {
  margin: 0;
}

.chart-row {
  display: grid;
  grid-template-columns: minmax(58px, 0.8fr) minmax(100px, 1.5fr) auto;
  gap: 10px;
  align-items: center;
  font-size: 13px;
}

.chart-track {
  height: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--soft);
}

.chart-track i {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
}

.chart-track i.negative {
  background: #b23a48;
}

.chart-track i.category {
  background: #d89c45;
}

.chart-row b {
  white-space: nowrap;
}

.recurring-confirm-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.compact-heading {
  margin-bottom: 0;
}

.compact-heading h3 {
  margin: 0;
}

.item-rows {
  display: grid;
  gap: 8px;
}

.item-row {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(90px, 0.8fr) 42px;
  gap: 8px;
  align-items: center;
}

.item-row .icon-button {
  min-height: 42px;
}

.pill-list {
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}

.pill {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--soft);
  padding: 10px 12px;
  font-weight: 800;
}

.pill-deletable {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px 6px 12px;
}

.pill-delete-btn {
  background: none;
  color: var(--muted);
  font-size: 16px;
  font-weight: 400;
  min-height: 24px;
  width: 24px;
  padding: 0;
  border-radius: 50%;
  line-height: 1;
  flex-shrink: 0;
}
.pill-delete-btn:hover {
  background: var(--danger);
  color: #fff;
}

.empty-state {
  display: grid;
  gap: 8px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--soft);
  padding: 18px;
}

.empty-state p {
  margin: 0;
  color: var(--muted);
}

.bottom-nav {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  width: min(1080px, 100%);
  transform: translateX(-50%);
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(14px);
  padding: 6px 8px max(6px, env(safe-area-inset-bottom));
}

.bottom-nav button {
  display: grid;
  gap: 2px;
  justify-items: center;
  min-height: 52px;
  padding: 4px;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
}

.bottom-nav button span {
  font-size: 20px;
  line-height: 1;
}

.bottom-nav button.active {
  background: var(--soft);
  color: var(--accent-strong);
}

.fab {
  position: fixed;
  left: 50%;
  bottom: max(62px, calc(62px + env(safe-area-inset-bottom)));
  z-index: 25;
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  min-height: 58px;
  padding: 0;
  transform: translateX(-50%);
  border: 4px solid var(--bg);
  border-radius: 50%;
  background: #d89c45;
  color: #1d1308;
  font-size: 30px;
  line-height: 1;
  box-shadow: 0 16px 32px rgba(82, 53, 18, 0.24);
}

.fab:hover {
  background: #c98532;
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  align-items: end;
  background: rgba(15, 25, 21, 0.42);
  padding: 16px 16px max(16px, env(safe-area-inset-bottom));
}

.sheet-panel {
  width: min(560px, 100%);
  max-height: min(86dvh, 760px);
  margin: 0 auto;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 24px 80px rgba(20, 32, 27, 0.28);
  padding: 10px 18px 18px;
}

.sheet-handle {
  width: 42px;
  height: 5px;
  margin: 0 auto 10px;
  border-radius: 999px;
  background: #c9d8d1;
}

.sheet-open {
  overflow: hidden;
}

@media (min-width: 820px) {
  .app-shell {
    width: min(1200px, calc(100% - 32px));
    padding: 18px 0;
  }

  #appView:not([hidden]) {
    display: grid;
    grid-template-columns: 92px minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    gap: 0 18px;
    align-items: start;
  }

  .topbar {
    grid-column: 2;
    grid-row: 1;
  }

  .app-frame {
    grid-column: 2;
    grid-row: 2;
    height: calc(100dvh - 112px);
    padding-bottom: 0;
  }

  .bottom-nav {
    position: sticky;
    top: 18px;
    left: auto;
    bottom: auto;
    z-index: 1;
    grid-column: 1;
    grid-row: 1 / 3;
    display: flex;
    flex-direction: column;
    width: 92px;
    height: calc(100dvh - 36px);
    transform: none;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.92);
    padding: 10px;
    box-shadow: var(--shadow);
    border-top: none;
  }

  .bottom-nav button {
    min-height: 70px;
  }

  .fab {
    left: auto;
    right: max(28px, calc((100vw - 1200px) / 2 + 28px));
    bottom: 28px;
    transform: none;
  }

  .dashboard-scroll,
  .list-screen,
  .workspace-grid {
    padding-bottom: 24px;
  }

  .workspace-grid > .panel {
    overflow: visible;
  }
}

@media (max-width: 820px) {
  body {
    overflow: hidden;
  }

  .app-shell {
    width: 100%;
    height: 100dvh;
    min-height: 0;
    padding: 12px 10px 0;
    overflow: hidden;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
    min-height: 88px;
  }

  .top-actions {
    justify-content: space-between;
  }

  .summary-grid,
  .workspace-grid {
    grid-template-columns: 1fr;
  }

  #appView {
    height: 100%;
    overflow: hidden;
  }

  .app-frame {
    height: calc(100% - 156px);
    overflow: hidden;
    padding-bottom: 0;
  }

  .tab-panel.active,
  .workspace-grid {
    height: 100%;
    max-height: none;
    overflow: hidden;
  }

  .dashboard-scroll,
  .list-screen,
  .workspace-grid {
    height: 100%;
    max-height: none;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }

  .workspace-grid {
    align-content: start;
  }

  .workspace-grid > .panel {
    height: auto;
    min-height: 0;
    max-height: none;
    overflow: visible;
  }

  .summary-card {
    min-height: 64px;
    padding: 13px;
  }

  .summary-grid,
  .summary-grid.compact {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .hero-balance {
    padding: 18px 20px;
  }

  .screen-heading,
  .section-heading {
    align-items: stretch;
    flex-direction: column;
  }

  .sheet-backdrop {
    padding: 0;
  }

  .sheet-panel {
    width: 100%;
    max-height: 92dvh;
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
  }

  .recurring-confirm-grid {
    grid-template-columns: 1fr;
  }

  .chart-panel {
    grid-template-columns: 1fr;
  }

  .chart-row {
    grid-template-columns: minmax(52px, 0.7fr) minmax(80px, 1fr);
  }

  .chart-row b {
    grid-column: 2;
  }
}

/* Stable app scrolling: vertical scroll on body; NO horizontal scroll ever */
html {
  overflow-x: hidden;
}

body {
  overflow-x: hidden;
  overflow-y: auto;
}

.app-shell {
  height: auto;
  min-height: 100dvh;
  overflow-x: hidden;
  overflow-y: visible;
  padding-bottom: 94px;
  /* Ensure shell never exceeds viewport width */
  max-width: 100vw;
  box-sizing: border-box;
}

#appView,
.app-frame,
.tab-panel.active,
.dashboard-scroll,
.list-screen,
.workspace-grid {
  height: auto;
  min-height: 0;
  max-height: none;
  overflow: visible;
}

.tab-panel.active {
  display: block;
}

.workspace-grid {
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.workspace-grid > .panel {
  overflow: visible;
}

.sheet-open {
  overflow: hidden;
}

@media (min-width: 1100px) {
  .workspace-grid {
    display: grid;
    grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
    align-items: start;
  }
}

@media (min-width: 820px) {
  .app-frame {
    min-height: calc(100dvh - 112px);
  }
}

.record-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.edit-button {
  opacity: 0;
  font-size: 14px;
  padding: 2px 6px;
  transition: opacity 0.15s;
}

.record:hover .edit-button,
.record:focus-within .edit-button {
  opacity: 1;
}

@media (pointer: coarse) {
  .edit-button { opacity: 0.6; }
}

.danger-button {
  color: var(--red);
}

.chart-legend {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 10px;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}

.legend-row i {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.legend-row span { flex: 1; font-size: 12px; }
.legend-row .legend-amount { flex: none; color: var(--muted); font-size: 12px; }
.legend-row b { color: var(--muted); font-size: 12px; min-width: 28px; text-align: right; }

.donut-wrapper {
  width: 100%;
  max-width: 220px;
  margin: 0 auto;
  position: relative;
}

#categoryDonut {
  display: block;
  width: 100% !important;
  height: auto !important;
}

/* topbar title left-aligned */
.topbar {
  text-align: left;
}

/* home section spacing */
.home-section {
  margin-top: 16px;
}

/* collapsible details on homepage */
.home-section details summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  user-select: none;
}
.home-section details summary::marker,
.home-section details summary::-webkit-details-marker {
  display: none;
}
.home-section details summary h2::after {
  content: " ▸";
  font-size: 12px;
  opacity: 0.5;
}
.home-section details[open] summary h2::after {
  content: " ▾";
}
.home-section details > *:not(summary) {
  margin-top: 8px;
}

/* liability payment button */
.compact-button {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
}

/* ── home section spacing ── */
.home-section {
  margin-top: 24px !important;
}

/* ── collapsible recurring heading ── */
.collapsible-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.collapsible-heading::-webkit-details-marker { display: none; }
.collapsible-heading::marker { display: none; }
.collapse-arrow {
  font-size: 13px;
  opacity: 0.5;
  transition: transform 0.2s;
}
details[open] .collapse-arrow { transform: rotate(90deg); }
details[open] #recurringDueList { margin-top: 10px; }

/* ── investment list gap ── */
.investment-tabs { margin-bottom: 16px; }
.investment-list-gap { margin-top: 8px; }

/* ── Apple-style calculator operator buttons ── */
.calculator-pad .op-button {
  background: #ff9f0a;
  color: #fff;
  font-weight: 600;
}
.calculator-pad .op-button:hover { background: #ffc547; }
.calculator-pad .equals {
  background: #ff9f0a;
  color: #fff;
  font-weight: 600;
}
.calculator-pad .equals:hover { background: #ffc547; }

/* ── profile settings hr ── */
.stack hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 8px 0;
}
.stack h3 { margin: 4px 0 0; font-size: 15px; }

/* ── recurring rule list ── */
.recurring-rule-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.recurring-rule-row:last-child { border-bottom: none; }
.recurring-rule-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.recurring-rule-title { font-weight: 500; font-size: 15px; }
.recurring-rule-amount { font-size: 14px; }

/* ── portfolio header ── */
.portfolio-header {
  display: flex;
  gap: 16px;
  padding: 16px 0 8px;
}
.portfolio-stat {
  flex: 1;
  background: var(--soft);
  border-radius: 12px;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.portfolio-stat-label { font-size: 12px; color: var(--muted, #888); }
.portfolio-stat-value { font-size: 18px; font-weight: 600; }

/* ── position cards ── */
.position-card {
  background: var(--soft);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 12px;
}
.position-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 12px;
}
.position-identity { display: flex; flex-direction: column; gap: 2px; }
.position-symbol { font-size: 18px; font-weight: 700; letter-spacing: 0.5px; }
.position-name { font-size: 13px; color: var(--muted, #888); }
.position-quantity { font-size: 14px; font-weight: 500; color: var(--muted, #888); }
.position-metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.position-metric {
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: #fff;
  border-radius: 10px;
  padding: 8px 10px;
}
.position-metric-label { font-size: 11px; color: var(--muted, #888); }
.position-metric-value { font-size: 13px; font-weight: 600; }

/* ── trade rows ── */
.trade-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.trade-row:last-child { border-bottom: none; }
.trade-badge {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  flex-shrink: 0;
}
.trade-badge.buy { background: #e8f5e9; color: #2e7d32; }
.trade-badge.sell { background: #fce4ec; color: #c62828; }
.trade-info { flex: 1; min-width: 0; }
.trade-title { font-size: 14px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.trade-symbol { font-weight: 700; }
.trade-meta { font-size: 12px; color: var(--muted, #888); margin-top: 2px; }
.trade-amount { font-size: 14px; font-weight: 600; flex-shrink: 0; }

/* ── settlement warning ── */
.settlement-warning { border-color: #f59e0b !important; background: #fffbeb !important; }
.settlement-alert { color: #d97706; font-weight: 600; }

.alert-hit-list {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}

.alert-hit-list span {
  display: inline-block;
  border: 1px solid #f0c7ce;
  border-radius: 8px;
  background: #fff4f5;
  color: var(--danger);
  padding: 7px 9px;
  font-weight: 800;
}

/* ── hero invest row ── */
.hero-invest-row { font-size: 13px; opacity: 0.85; margin-top: 4px; }


/* ── shared person rows ── */
.shared-person-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.shared-person-row .shared-person-name { flex: 1; }
.shared-person-row .shared-person-amount { width: 90px; flex-shrink: 0; }

/* ── FAB menu ── */
.fab-menu {
  position: fixed;
  bottom: 92px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-end;
  z-index: 200;
}
.fab-option {
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 0 18px;
  min-height: 44px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  white-space: nowrap;
}
.fab-option:hover { background: var(--soft); }

/* ── invest tab row ── */
.invest-tab-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  flex-wrap: wrap;
}
.invest-tab-row .investment-tabs { margin-bottom: 0; flex-shrink: 1; }
#chartTypeTabs { flex-shrink: 0; }
#chartTypeTabs button { min-width: 36px; padding: 0 10px; }

/* ── Quick trade button on position card ── */
.position-quick-btn {
  margin-top: 10px;
  width: 100%;
  background: var(--soft);
  color: var(--accent-strong);
  border: 1px solid var(--line);
  font-size: 13px;
  min-height: 36px;
}
.position-quick-btn:hover { background: #dce8e3; }

/* ── Performance tab ── */
.perf-toggle { margin-bottom: 12px; }
.perf-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.perf-table th { text-align: left; color: var(--muted); font-weight: 700; padding: 6px 8px; border-bottom: 2px solid var(--line); }
.perf-table td { padding: 8px 8px; border-bottom: 1px solid var(--line); }
.perf-table tr:last-child td { border-bottom: none; }
.perf-table .perf-positive { color: #13704f; font-weight: 700; }
.perf-table .perf-negative { color: var(--danger); font-weight: 700; }

.perf-summary-stats { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.perf-chart-card {
  background: var(--chart-card);
  border-radius: 12px;
  padding: 14px 14px 10px;
  margin-bottom: 14px;
}
#perfRealizedCanvas { width: 100% !important; height: 120px !important; display: block; }

/* Analysis tab */
.analysis-period-row { display: flex; gap: 8px; margin-bottom: 4px; }
.analysis-period-row select { flex: 1; }
#analysisChartContainer canvas { max-height: 260px; }
#analysisMainCanvas { width: 100% !important; }

/* ── Monthly analysis chart ── */
.analysis-section { margin-top: 14px; }
.analysis-bar-row { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; font-size: 12px; }
.analysis-bar-label { width: 52px; color: var(--muted); text-align: right; flex-shrink: 0; }
.analysis-bar-track { flex: 1; height: 16px; background: var(--line); border-radius: 6px; overflow: hidden; position: relative; display: flex; }
.analysis-bar-income { background: var(--accent); height: 100%; border-radius: 6px; transition: width 0.3s; }
.analysis-bar-expense { background: var(--danger); height: 100%; border-radius: 6px; transition: width 0.3s; opacity: 0.75; }
.analysis-bar-val { font-size: 11px; color: var(--muted); white-space: nowrap; }

/* ── invest chart area ── */
#investmentChartArea {
  margin: 12px 0 4px;
}
#investmentChart {
  display: block;
  width: 100% !important;
  max-width: 260px;
  height: auto !important;
  margin: 0 auto;
}

/* ── invest bar summary ── */
.invest-bar-row {
  display: grid;
  grid-template-columns: 80px 1fr;
  gap: 8px;
  align-items: start;
  margin-bottom: 14px;
}
.invest-bar-label {
  font-size: 13px;
  font-weight: 700;
  padding-top: 4px;
  word-break: break-all;
}
.invest-bar-tracks { display: flex; flex-direction: column; gap: 4px; }
.invest-bar-track {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 20px;
}
.invest-bar-fill {
  display: inline-block;
  height: 8px;
  border-radius: 999px;
  min-width: 4px;
  flex-shrink: 0;
}
.invest-bar-val { font-size: 12px; white-space: nowrap; }
.invest-bar-note { font-size: 12px; font-weight: 600; padding-left: 2px; }

/* ── Portfolio line chart card ── */
.portfolio-line-card {
  background: var(--chart-card);
  border-radius: 16px;
  padding: 20px 16px 12px;
  margin-bottom: 12px;
}
.plc-header { margin-bottom: 12px; }
.plc-total {
  font-size: 28px;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.5px;
}
.plc-gain {
  font-size: 13px;
  font-weight: 600;
  margin-top: 2px;
}
.plc-gain.positive { color: var(--chart-line); }
.plc-gain.negative { color: #f87171; }
.plc-note { font-size: 11px; margin-top: 2px; color: #8ab5a0; }
#portfolioLineCanvas {
  width: 100% !important;
  height: 140px !important;
  display: block;
}
.plc-periods {
  display: flex;
  justify-content: space-around;
  margin-top: 14px;
  gap: 4px;
}
.plc-period-btn {
  background: none;
  border: none;
  color: #8ab5a0;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 20px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.plc-period-btn.active {
  background: var(--accent);
  color: #fff;
}

/* ── Auth link button ── */
.link-button {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 0;
  text-decoration: underline;
  display: block;
  margin: 4px auto 0;
}

/* ── Short position badge ── */
.position-card--short { border-left: 3px solid var(--danger); }
.short-badge {
  font-size: 10px;
  font-weight: 700;
  background: var(--danger);
  color: #fff;
  border-radius: 4px;
  padding: 1px 5px;
  vertical-align: middle;
}

/* ── position card enhancements ── */
.position-metrics {
  grid-template-columns: repeat(3, 1fr);
}
@media (max-width: 420px) {
  .position-metrics { grid-template-columns: repeat(2, 1fr); }
}

/* ── Themes ─────────────────────────────────────────────────────────────── */
/* 松綠（預設）*/
[data-theme="green"] {
  --bg: #edf3ef; --panel: #ffffff; --ink: #1e2d27; --muted: #6b8078;
  --line: #d4e2db; --accent: #2e7d5e; --accent-strong: #1d5e44;
  --soft: #f3f8f5; --chart-line: #5ec49a; --chart-card: #1e3d30;
}
/* 靛藍 */
[data-theme="blue"] {
  --bg: #edf1f7; --panel: #ffffff; --ink: #1a263e; --muted: #5e728f;
  --line: #ccd6e8; --accent: #3560a0; --accent-strong: #244880;
  --soft: #f2f5fb; --chart-line: #6096d8; --chart-card: #1a2e50;
}
/* 霧紫 */
[data-theme="purple"] {
  --bg: #f0edf8; --panel: #ffffff; --ink: #281e40; --muted: #7464a0;
  --line: #dcd4f0; --accent: #6248b0; --accent-strong: #4a3490;
  --soft: #f6f3fc; --chart-line: #9880d8; --chart-card: #281e48;
}
/* 磚橘 */
[data-theme="clay"] {
  --bg: #f5efea; --panel: #ffffff; --ink: #32220e; --muted: #8a6a50;
  --line: #e0cfc0; --accent: #b86040; --accent-strong: #924828;
  --soft: #faf5f0; --chart-line: #e0a070; --chart-card: #321808;
}
/* 石板 */
[data-theme="gray"] {
  --bg: #edf0f4; --panel: #ffffff; --ink: #1a2230; --muted: #5e7090;
  --line: #ccd4e0; --accent: #446080; --accent-strong: #2c4866;
  --soft: #f2f4f8; --chart-line: #7098c0; --chart-card: #1a2838;
}

/* ── Theme swatches ─────────────────────────────────────────────────────────── */
.theme-swatches {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin: 4px 0 8px;
}
.theme-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  padding: 0;
  transition: transform 0.15s;
}
.theme-swatch.active {
  border-color: var(--ink);
  transform: scale(1.15);
}
.theme-swatch:hover { transform: scale(1.1); }

/* ── Mobile UX fixes ─────────────────────────────────────────────────────── */

/* iOS Safari auto-zoom prevention: inputs must be ≥ 16px */
@media (max-width: 820px) {
  input, select, textarea {
    font-size: 16px;
    /* Prevent any field from overflowing its container */
    max-width: 100%;
    box-sizing: border-box;
  }

  /* More breathing room on sides — equal left & right */
  .app-shell {
    padding-left: 18px !important;
    padding-right: 18px !important;
  }

  /* Give scroll containers proper inset so nothing hugs the edge */
  .dashboard-scroll,
  .list-screen,
  .workspace-grid {
    padding-left: 2px;
    padding-right: 2px;
    padding-bottom: 80px; /* extra space above bottom nav */
  }

  /* Hero balance: prevent overflow with large numbers */
  .hero-dual-item {
    min-width: 0;
    overflow: hidden;
  }

  /* Summary cards: never let numbers overflow */
  .summary-card strong {
    font-size: clamp(16px, 5vw, 22px);
    word-break: break-all;
  }
}

/* ── Date input: prevent iOS intrinsic min-width from causing horizontal scroll */
input[type="date"],
input[type="time"],
input[type="datetime-local"] {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
  /* Override iOS native appearance so it obeys CSS width */
  -webkit-appearance: none;
  appearance: none;
}

/* Labels are display:grid — grid children default to min-width:auto which
   lets date inputs expand past the container. Fix: min-width:0 on labels */
label {
  min-width: 0;
}
label > input,
label > select,
label > textarea {
  min-width: 0;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

/* ── Form field alignment ─────────────────────────────────────────────────── */
/* All sheet panels: consistent padding so fields have breathing room */
.sheet-panel {
  padding-left: 20px;
  padding-right: 20px;
  overflow-x: hidden; /* clip anything that tries to escape */
}

/* All form fields: uniform full width, no overflow */
.sheet-panel input,
.sheet-panel select,
.sheet-panel textarea {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

/* Labels: consistent spacing above each field */
.sheet-panel label {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

/* segmented-control in forms: don't overflow */
.sheet-panel .segmented-control {
  width: 100%;
  box-sizing: border-box;
  flex-wrap: wrap;
}

/* Calculator pad: don't overflow its container */
.calculator-pad {
  width: 100%;
  box-sizing: border-box;
}

/* subform sections: same inset as the sheet */
.sheet-panel .subform {
  overflow: hidden;
}

/* ── Bigger, more readable investment cards ─────────────────────────────── */
.position-symbol { font-size: 20px; font-weight: 800; }
.position-name   { font-size: 14px; }
.position-quantity { font-size: 13px; }

/* Always 2-column for position metrics — more breathing room */
.position-metrics {
  grid-template-columns: repeat(2, 1fr) !important;
  gap: 10px;
}

.position-metric {
  padding: 12px 14px;
  border-radius: 12px;
}
.position-metric-label {
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.position-metric-value { font-size: 16px; font-weight: 700; }

.position-card {
  padding: 18px;
  margin-bottom: 14px;
  border-radius: 18px;
}

/* Portfolio summary stats: 2×2 grid on mobile */
@media (max-width: 820px) {
  .portfolio-header {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }
}

/* portfolio-stat larger text */
.portfolio-stat {
  padding: 14px 16px;
}
.portfolio-stat-value { font-size: 20px; font-weight: 700; }
.portfolio-stat-label { font-size: 13px; }

/* trade list larger */
.trade-title { font-size: 15px; }
.trade-meta  { font-size: 13px; }
.trade-amount { font-size: 15px; }
