/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--control-h); padding: 0 var(--space-4);
  border-radius: var(--radius-md); border: 1px solid transparent;
  font-size: var(--text-sm); font-weight: 650; line-height: 1; white-space: nowrap; text-decoration: none;
  transition: background .15s, border-color .15s, color .15s;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--primary-fill); color: var(--on-primary-fill); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-gain { background: var(--gain); color: var(--on-primary-fill); }
:root:not([data-theme="light"]) .btn-gain { color: #111827; }
.btn-secondary { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.btn-secondary:hover:not(:disabled) { border-color: var(--border-strong); background: var(--surface-3); }
.btn-ghost { color: var(--text-muted); }
.btn-ghost:hover:not(:disabled) { color: var(--text); background: var(--surface-2); }
.btn-danger { background: var(--loss-soft); color: var(--loss); border-color: color-mix(in srgb, var(--loss) 35%, transparent); }
.btn-sm { min-height: var(--control-h-sm); padding: 0 var(--space-3); font-size: var(--text-xs); border-radius: var(--radius-sm); }
.btn-block { width: 100%; }
.link-btn { color: var(--primary-text); font-weight: 650; text-decoration: underline; text-underline-offset: 2px; font-size: inherit; min-height: 0; }
.icon-btn {
  display: inline-grid; place-items: center; flex-shrink: 0;
  width: var(--control-h); height: var(--control-h);
  border-radius: var(--radius-pill); border: 1px solid var(--border);
  background: var(--surface); color: var(--text-muted);
}
.icon-btn:hover:not(:disabled) { color: var(--text); border-color: var(--border-strong); }
.icon-btn:disabled { opacity: .35; cursor: not-allowed; }
.icon-btn-sm { width: var(--control-h-sm); height: var(--control-h-sm); }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); padding: var(--space-5); }
.card-tight { padding: var(--space-4); }
.card-title { font-size: var(--text-md); font-weight: 700; letter-spacing: -.01em; }
.card-subtitle { color: var(--text-muted); font-size: var(--text-sm); margin-top: 2px; }
.eyebrow { font-size: var(--text-xs); font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--text-muted); }

/* Metrics */
.metric-label { font-size: var(--text-xs); font-weight: 600; color: var(--text-muted); }
.metric-value { font-size: var(--text-2xl); font-weight: 800; letter-spacing: -.03em; line-height: 1.15; font-variant-numeric: tabular-nums; }
.metric-value-sm { font-size: var(--text-lg); font-weight: 750; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.metrics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
@media (min-width: 640px) { .metrics { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.metric { background: var(--surface-2); border-radius: var(--radius-md); padding: var(--space-3); min-width: 0; }

/* Forms */
.field { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
.field-label { font-size: var(--text-xs); font-weight: 650; color: var(--text-muted); }
.field-hint { font-size: var(--text-xs); color: var(--text-muted); }
.field-error { font-size: var(--text-xs); color: var(--loss); font-weight: 600; }
.field-row { display: grid; grid-template-columns: minmax(120px, 1fr) auto; gap: var(--space-2); align-items: end; }
.field-row .select { width: auto; }
.field-row .cluster { flex-wrap: nowrap; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: var(--space-3); }
.input, .select, .textarea {
  width: 100%; min-height: var(--control-h);
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-md);
  padding: 0 var(--space-3); font-size: var(--text-md);
}
.textarea { padding: var(--space-3); min-height: 80px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.input[aria-invalid="true"] { border-color: var(--loss); }
.select-sm { min-height: var(--control-h-sm); padding: 0 var(--space-2); font-size: var(--text-sm); border-radius: var(--radius-sm); }
.input-affix { position: relative; display: block; }
.input-affix .affix { position: absolute; left: var(--space-3); top: 50%; transform: translateY(-50%); color: var(--text-muted); pointer-events: none; }
.input-affix .affix svg { display: block; }
.input-affix .input { padding-left: 38px; }
.check { display: inline-flex; align-items: center; gap: var(--space-2); min-height: var(--control-h); font-size: var(--text-sm); cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }
.checks { display: flex; flex-wrap: wrap; gap: 0 var(--space-4); }

/* Segmented control */
.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: var(--space-1); padding: var(--space-1); background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-md); }
.segmented button { min-height: 36px; border-radius: var(--radius-sm); font-size: var(--text-sm); font-weight: 650; color: var(--text-muted); }
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

/* Chips */
.chip { display: inline-flex; align-items: center; gap: var(--space-1); padding: 2px var(--space-2); border-radius: var(--radius-pill); font-size: var(--text-xs); font-weight: 700; line-height: 1.5; white-space: nowrap; background: var(--surface-3); color: var(--text-muted); }
.chip .icon { width: 13px; height: 13px; }
.chip-gain { background: var(--gain-soft); color: var(--gain); }
.chip-warn { background: var(--warn-soft); color: var(--warn); }
.chip-loss { background: var(--loss-soft); color: var(--loss); }
.chip-primary { background: var(--primary-soft); color: var(--primary-text); }
.chip-gain, .chip-warn, .chip-loss, .chip-primary { box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 35%, transparent); }
.rule-check.is-fail { box-shadow: inset 3px 0 0 var(--loss); }

/* Lists */
.list { display: flex; flex-direction: column; gap: var(--space-2); }
.list-item {
  display: block; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: var(--space-3) var(--space-4); box-shadow: var(--shadow-sm);
}
a.list-item { color: inherit; text-decoration: none; }
button.list-item, a.list-item { cursor: pointer; }
button.list-item:hover, a.list-item:hover { border-color: var(--border-strong); }
.rule-body { color: inherit; text-decoration: none; display: flex; flex-direction: column; justify-content: center; }
.list-item-top { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-3); }
.list-item-title { font-weight: 650; font-size: var(--text-sm); min-width: 0; overflow-wrap: anywhere; }
.list-item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-1) var(--space-3); margin-top: var(--space-1); font-size: var(--text-xs); color: var(--text-muted); }
.list-item-amount { font-weight: 750; font-size: var(--text-sm); white-space: nowrap; font-variant-numeric: tabular-nums; text-align: right; }

.list-view { display: flex; flex-direction: column; gap: var(--space-3); scroll-margin-top: 80px; }
.list-controls { display: flex; flex-direction: column; gap: var(--space-2); }
.list-controls-row { display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--space-2); }
.list-filter, .list-sort { display: flex; flex-direction: column; gap: 2px; flex: 1 1 120px; min-width: 0; }
.list-pager {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: var(--space-2) var(--space-4);
  padding: var(--space-2) var(--space-3); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md);
}
.pager-count { font-size: var(--text-xs); color: var(--text-muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.pager-pages { display: flex; flex-wrap: wrap; gap: var(--space-1); }
.pager-btn {
  display: inline-grid; place-items: center; min-width: var(--control-h); height: var(--control-h); padding: 0 var(--space-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2);
  font-size: var(--text-sm); font-weight: 600; font-variant-numeric: tabular-nums;
}
.pager-btn:hover:not(:disabled) { border-color: var(--primary); color: var(--primary-text); }
.pager-btn[aria-current="page"] { background: var(--primary-fill); border-color: var(--primary-fill); color: var(--on-primary-fill); }
.pager-btn:disabled { opacity: .35; cursor: not-allowed; }
.pager-size { display: flex; align-items: center; gap: var(--space-2); margin-left: auto; font-size: var(--text-xs); color: var(--text-muted); white-space: nowrap; }
.pager-size .select { width: auto; }
@media (max-width: 480px) { .pager-size { margin-left: 0; width: 100%; justify-content: center; } }

/* Empty, notices, skeletons, retry */
.empty { text-align: center; padding: var(--space-6) var(--space-4); color: var(--text-muted); font-size: var(--text-sm); border: 1px dashed var(--border); border-radius: var(--radius-md); }
.empty .cluster { justify-content: center; margin-top: var(--space-3); }
.hint { font-size: var(--text-sm); color: var(--text-muted); }
.notice { display: flex; gap: var(--space-3); align-items: flex-start; padding: var(--space-3) var(--space-4); border-radius: var(--radius-md); font-size: var(--text-sm); background: var(--primary-soft); }
.notice > .icon { color: var(--primary-text); margin-top: 2px; }
.notice-warn { background: var(--warn-soft); }
.notice-warn > .icon { color: var(--warn); }
.notice-loss { background: var(--loss-soft); }
.notice-loss > .icon { color: var(--loss); }
.skeleton { border-radius: var(--radius-md); background: linear-gradient(90deg, var(--surface-2), var(--surface-3), var(--surface-2)); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
.skeleton-line { height: 14px; margin: var(--space-2) 0; }
.skeleton-block { height: 120px; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

/* Sheets: bottom sheet on phones, centred dialog from 640px */
.overlay { position: fixed; inset: 0; z-index: var(--z-sheet); background: var(--overlay); display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 640px; max-height: 92dvh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0; box-shadow: var(--shadow-lg);
  padding: var(--space-5) var(--space-4) calc(var(--space-5) + env(safe-area-inset-bottom));
  animation: sheet-in .2s var(--ease);
}
.sheet-sm { max-width: 440px; }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-3); margin-bottom: var(--space-4); }
.sheet-title { font-size: var(--text-lg); font-weight: 750; letter-spacing: -.01em; overflow-wrap: anywhere; }
.sheet-body { display: flex; flex-direction: column; gap: var(--space-5); }
.sheet-section { display: flex; flex-direction: column; gap: var(--space-3); padding-top: var(--space-4); border-top: 1px solid var(--border); }
.sheet-foot { display: flex; flex-wrap: wrap; gap: var(--space-2); }
@keyframes sheet-in { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
@media (min-width: 640px) {
  .overlay { align-items: center; padding: var(--space-6); }
  .sheet { border-radius: var(--radius-lg); padding: var(--space-6); }
}

/* Toasts */
.toast-region { position: fixed; z-index: var(--z-toast); left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: var(--space-2); width: min(92vw, 420px); pointer-events: none; }
.toast { padding: var(--space-3) var(--space-4); border-radius: var(--radius-md); background: var(--text); color: var(--bg); font-size: var(--text-sm); font-weight: 600; box-shadow: var(--shadow-md); }
.toast-loss { background: var(--loss); color: #fff; }
@media (min-width: 900px) { .toast-region { bottom: var(--space-6); } }

/* Sign-in */
.auth { position: fixed; inset: 0; z-index: var(--z-auth); display: grid; place-items: center; padding: var(--space-4); background: var(--bg); overflow-y: auto; }
.auth-card { width: 100%; max-width: 380px; }
.auth-card .brand-mark { width: 56px; height: 56px; margin: 0 auto var(--space-4); display: block; }
.auth-title { font-size: var(--text-2xl); font-weight: 800; letter-spacing: -.03em; text-align: center; }
.auth-title span { color: var(--primary-text); }
.auth-copy { color: var(--text-muted); margin: var(--space-2) 0 var(--space-6); font-size: var(--text-sm); text-align: center; }

/* Charts */
.chart-box { position: relative; height: 260px; }
.chart-box-sm { height: 200px; }
.chart-nav { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3); margin-top: var(--space-3); }
.chart-zoom { display: flex; align-items: center; gap: var(--space-1); }
.chart-hint { font-size: var(--text-xs); color: var(--text-muted); }

/* Allocation */
.alloc-bar { display: flex; height: 14px; border-radius: var(--radius-pill); overflow: hidden; background: var(--surface-3); gap: 2px; }
.alloc-seg { height: 100%; }
.alloc-legend { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); margin-top: var(--space-3); font-size: var(--text-sm); }
.alloc-key { display: inline-flex; align-items: center; gap: var(--space-2); }
.alloc-key::before { content: ''; width: 10px; height: 10px; border-radius: 3px; background: var(--key); }
.alloc-stock { --key: var(--alloc-stock); background: var(--alloc-stock); }
.alloc-etf { --key: var(--alloc-etf); background: var(--alloc-etf); }
.alloc-fund { --key: var(--alloc-fund); background: var(--alloc-fund); }
.alloc-cash { --key: var(--alloc-cash); background: var(--alloc-cash); }
.alloc-key.alloc-stock, .alloc-key.alloc-etf, .alloc-key.alloc-fund, .alloc-key.alloc-cash { background: none; }

/* Cash-flow table */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-md); }
.table { width: 100%; font-size: var(--text-sm); font-variant-numeric: tabular-nums; }
.table th, .table td { padding: var(--space-2) var(--space-3); text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table th:first-child, .table td:first-child { text-align: left; }
.table thead th { font-size: var(--text-xs); color: var(--text-muted); font-weight: 650; background: var(--surface-2); }
.table tfoot td { font-weight: 750; border-bottom: none; }
.table .muted td { color: var(--text-muted); }

/* Rules checklist */
.rule-checks { display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; }
.rule-check { display: flex; gap: var(--space-3); align-items: flex-start; width: 100%; min-height: var(--control-h); padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); font-size: var(--text-sm); text-align: left; background: transparent; }
.rule-check:last-child { border-bottom: none; }
.rule-check > .icon { margin-top: 1px; }
button.rule-check:hover { background: var(--surface-2); }
.rule-check-main { flex: 1; min-width: 0; }
.rule-check-text { font-weight: 600; overflow-wrap: anywhere; }
.rule-check-why { font-size: var(--text-xs); color: var(--text-muted); margin-top: 2px; overflow-wrap: anywhere; }
.rule-check.is-pass > .icon { color: var(--gain); }
.rule-check.is-fail { background: var(--loss-soft); }
.rule-check.is-fail > .icon, .rule-check.is-fail .rule-check-why { color: var(--loss); }
.rule-check.is-tick > .icon, .rule-check.is-reminder > .icon { color: var(--text-muted); }
.tickbox { display: inline-grid; place-items: center; width: 18px; height: 18px; flex-shrink: 0; margin-top: 1px; border: 2px solid var(--border-strong); border-radius: 5px; color: var(--on-primary-fill); }
.tickbox .icon { width: 12px; height: 12px; stroke-width: 3; }
.rule-check[aria-checked="true"] .tickbox { background: var(--primary-fill); border-color: var(--primary-fill); }
.rule-check[aria-checked="false"] .tickbox .icon { visibility: hidden; }

/* Rules screen */
.rule-row { display: flex; align-items: center; gap: var(--space-2); padding: var(--space-2); }
.rule-row.dragging { opacity: .5; }
.rule-row.drop-target { border-color: var(--primary); }
.rule-handle { cursor: grab; color: var(--text-muted); display: inline-grid; place-items: center; width: 28px; height: var(--control-h); flex-shrink: 0; }
.rule-body { flex: 1; min-width: 0; text-align: left; min-height: var(--control-h); border-radius: var(--radius-sm); padding: 0 var(--space-1); }
.rule-order { display: flex; gap: var(--space-1); }

/* Stocks rows */
.stock-figures { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--space-2); margin-top: var(--space-2); font-size: var(--text-xs); color: var(--text-muted); }
.stock-figures strong:not([class]) { color: var(--text); }
.stock-figures strong { display: block; font-size: var(--text-sm); font-variant-numeric: tabular-nums; }
.save-state { font-size: var(--text-xs); color: var(--text-muted); min-height: 18px; }
.save-state.is-error { color: var(--loss); }

details.disclosure > summary { cursor: pointer; list-style: none; display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); min-height: var(--control-h); padding: 0 var(--space-3); border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--surface-2); font-size: var(--text-sm); font-weight: 600; }
details.disclosure > summary::-webkit-details-marker { display: none; }
details.disclosure[open] > summary { margin-bottom: var(--space-3); }
