@layer base, shell, components, responsive;

@layer base {
  :root {
    --canvas: #f6f5fa;
    --paper: #ffffff;
    --ink: #221d32;
    --muted: #655d76;
    --stroke: #e6e0ec;
    --lavender: #eae5f8;
    --rail: #211b30;
    --rail-light: #d8cce9;
    --purple: #51378d;
    --purple-dark: #3c286d;
    --copper: #9b4130;
    --coral: #f1ae94;
    --green: #1d6754;
    --soft-green: #b8e6d9;
    --radius: 20px;
    --sm-radius: 12px;
    --space: clamp(16px, 3vw, 34px);
    --shadow: 0 12px 36px rgba(38,24,65,.045);
    --ease-out: cubic-bezier(.18,.76,.26,1);
    color-scheme: light;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Malgun Gothic", sans-serif;
    color: var(--ink);
    background: var(--canvas);
  }
  *, *::before, *::after { box-sizing: border-box; }
  [hidden] { display: none !important; }
  body { margin: 0; font-size: 1rem; line-height: 1.55; }
  h1, h2, h3, p { margin: 0; }
  h1 { font-size: clamp(1.85rem, 4vw, 2.75rem); font-weight: 770; letter-spacing: -.055em; line-height: 1.18; }
  h2 { font-size: 1.16rem; font-weight: 740; letter-spacing: -.035em; }
  h3 { font-size: 1rem; }
  button, input, textarea, select { font: inherit; color: inherit; }
  button { cursor: pointer; }
  button[disabled] { opacity: .42; cursor: default; }
  a { color: inherit; }
  :focus-visible { outline: 3px solid #7961b8; outline-offset: 3px; }
  :not(html) { scrollbar-color: #b8a9d7 transparent; }
}

@layer shell {
  .layout { min-height: 100vh; min-height: 100dvh; }
  .rail {
    background: var(--rail); color: white;
    padding: max(15px, env(safe-area-inset-top, 0px)) var(--space) 15px;
  }
  .brand { display: flex; gap: 11px; align-items: center; font-weight: 730; font-size: 1.05rem; letter-spacing: -.04em; }
  .brand img { width: 34px; height: 34px; border-radius: 10px; }
  .nav {
    z-index: 2; position: fixed; bottom: 0; inset-inline: 0; display: grid; grid-template-columns: repeat(4,1fr);
    background: var(--rail); padding: 5px 5px max(5px, env(safe-area-inset-bottom, 0px));
    border-top: 1px solid #51475e;
  }
  .nav-item {
    border: 0; background: none; color: var(--rail-light); min-height: 54px; border-radius: 12px;
    display: flex; flex-direction: column; gap: 2px; justify-content: center; align-items: center;
    font-size: .82rem; font-weight: 680; letter-spacing: -.025em;
  }
  .nav-item[aria-current] { background: #413455; color: white; }
  .nav-mark { font-size: 1rem; width: 22px; text-align: center; line-height: 1; }
  .rail-state { display: none; color: var(--rail-light); font-size: .85rem; }
  .workspace { min-width: 0; max-width: 1460px; margin-inline: auto; padding: var(--space) var(--space) calc(90px + env(safe-area-inset-bottom, 0px)); }
  .top { display: grid; gap: 17px; margin-bottom: 20px; }
  .title { min-width: 0; }
  .updated { margin-top: 4px; font-size: .87rem; color: var(--muted); overflow-wrap: anywhere; }
  .tools { min-width: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; }
  .tool-links { min-width: 0; display: flex; flex-wrap: wrap; gap: 3px; }
  .period { display: flex; align-items: center; gap: 4px; }
  .period select { padding-inline: 10px; min-width: 0; font-size: .92rem; }
  #content { display: grid; align-items: start; gap: 15px; min-width: 0; }
  #content:focus { outline: none; }
  .grid-two, .grid-spend, .stack { display: grid; align-content: start; align-items: start; gap: 15px; min-width: 0; }
  .skip {
    position: fixed; z-index: 10; top: 10px; left: 10px; background: white; padding: 10px; transform: translateY(-150%);
  }
  .skip:focus { transform: translateY(0); }
}

@layer components {
  .button, .icon-button, select {
    border: 1px solid var(--stroke); background: var(--paper); border-radius: var(--sm-radius);
    min-height: 44px; min-width: 44px;
  }
  .button {
    display: inline-flex; justify-content: center; align-items: center; padding: 6px 13px; text-decoration: none;
    font-size: .875rem; font-weight: 690; white-space: normal; text-align: center;
  }
  .icon-button { font-size: 1.35rem; line-height: 1; }
  .button.quiet { background: transparent; border-color: transparent; color: var(--purple-dark); padding-inline: 9px; }
  .button.danger { color: var(--copper); }
  .button.primary { background: var(--purple-dark); color: white; border-color: var(--purple-dark); }
  .button.pill { margin-top: 7px; background: var(--lavender); border-color: var(--lavender); color: var(--purple-dark); }
  select {
    max-width: 100%; white-space: nowrap; padding: 8px 27px 8px 11px; appearance: none;
    background-image: linear-gradient(45deg, transparent 48%, var(--muted) 49%), linear-gradient(135deg, var(--muted) 49%, transparent 50%);
    background-size: 5px 5px, 5px 5px; background-position: right 14px center, right 9px center; background-repeat: no-repeat;
  }
  .period select { padding-right: 23px; }
  .panel, .group {
    border: 1px solid var(--stroke); border-radius: var(--radius); background: var(--paper); box-shadow: var(--shadow); min-width: 0;
  }
  .panel { padding: clamp(14px, 2vw, 22px); }
  .panel-header { margin-bottom: 9px; }
  .panel-header p, .detail-hint, .search-count, .as-of, .lead-note { font-size: .875rem; color: var(--muted); }
  .group .detail-hint { text-align: right; }
  .group .money { justify-self: end; }
  .money, .row-value { font-variant-numeric: tabular-nums; }
  .as-of { padding-left: 4px; }
  .lead-card {
    position: relative; isolation: isolate; overflow: hidden;
    padding: clamp(20px, 4vw, 32px); min-width: 0; border-radius: var(--radius);
    background: radial-gradient(ellipse at 104% 9%, #684f8f 0%, transparent 59%), linear-gradient(125deg,#2c2242,#392854 70%,#4b346a);
    color: white; box-shadow: 0 12px 25px rgba(43,28,61,.18);
  }
  .lead-card::after {
    position: absolute; content: ""; z-index: -1; width: 285px; height: 285px; border-radius: 50%;
    right: -112px; top: -135px; border: 1px solid #a38cc1; box-shadow: 0 0 0 14px #ecd9ff16, 0 0 0 28px #ecd9ff13, 0 0 0 42px #ecd9ff0d;
  }
  .lead-main { display: grid; align-content: start; gap: 2px; }
  .lead-label { color: #e5d9f4; font-size: .95rem; font-weight: 650; }
  .balance {
    font-size: clamp(1.95rem, 7vw, 3.65rem); line-height: 1.15; letter-spacing: -.055em;
    font-variant-numeric: tabular-nums; overflow-wrap: anywhere;
  }
  .lead-note { color: #e5d9f4; }
  .lead-metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; padding-top: 16px; }
  .lead-metric {
    width: 100%; text-align: left; min-height: 56px; padding: 7px 8px; border-radius: 13px; min-width: 0;
    display: grid; gap: 2px; background: #fff1; color: white; border: 1px solid #ffffff36;
  }
  .lead-metric span { color: #e7daf7; font-size: .87rem; }
  .lead-metric strong { font-size: clamp(.8rem, 3.35vw, 1.2rem); line-height: 1.2; overflow-wrap: anywhere; }
  .groups { display: grid; grid-template-columns: 1fr; gap: 8px; }
  .group { text-align: left; padding: 12px 14px; display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 3px 10px; --accent: #805eb0; }
  .group.variable { --accent: #c96e53; }
  .group.one { --accent: #449b82; }
  .group-label { font-weight: 720; }
  .group .money { font-size: clamp(1rem, 2vw, 1.5rem); letter-spacing: -.04em; }
  meter { width: 100%; max-width: 100%; height: 8px; appearance: none; background: var(--stroke); border: 0; border-radius: 10px; }
  meter::-webkit-meter-bar { background: var(--stroke); border: 0; border-radius: 10px; }
  meter::-webkit-meter-optimum-value { background: var(--accent); border-radius: 10px; }
  meter::-moz-meter-bar { background: var(--accent); border-radius: 10px; }
  .data-row, .transaction {
    width: 100%; min-height: 53px; border: none; border-bottom: 1px solid var(--stroke); background: none; text-align: left;
    display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 7px 2px;
    font-size: .94rem;
  }
  .data-row:last-child, .transaction:last-child { border-bottom: none; }
  .row-label, .row-value { min-width: 0; display: grid; gap: 1px; }
  .row-label strong { overflow-wrap: anywhere; }
  .row-label small, .row-value small { font-size: .84rem; color: var(--muted); overflow-wrap: anywhere; line-height: 1.4; }
  .row-value, .fact-value { text-align: right; }
  .fact-value { overflow-wrap: anywhere; max-width: min(60vw, 300px); }
  .muted, .empty { color: var(--muted); font-size: .91rem; }
  .empty { background: var(--canvas); border-radius: var(--sm-radius); padding: 13px; margin: 7px 0; }
  .positive { color: var(--green); }
  .trend-legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: .85rem; color: var(--muted); }
  .trend-legend span { display: flex; gap: 5px; align-items: center; }
  .trend-legend span::before { content: ""; width: 9px; height: 9px; border-radius: 3px; background: var(--accent); }
  .legend-in, .bar.in { --accent: #4e8d7c; }
  .legend-out, .bar.out { --accent: #9172bc; }
  .trend { display: flex; gap: 2px; align-items: stretch; margin-top: 7px; }
  .trend-month {
    min-width: 0; min-height: 44px; flex: 1; display: grid; gap: 3px; grid-template-rows: minmax(95px, 1fr) auto;
    background: transparent; border: 0; border-radius: 12px; padding: 6px 2px; font-size: .81rem;
  }
  .trend-month[aria-current] { background: var(--lavender); color: var(--purple-dark); font-weight: 720; }
  .bars { display: flex; gap: 3px; justify-content: center; align-items: end; border-bottom: 1px solid var(--stroke); }
  .bar { display: block; height: var(--height); background: var(--accent); width: min(34%,18px); border-radius: 5px 5px 0 0; }
  .segment { display: flex; flex-wrap: wrap; gap: 4px; background: var(--lavender); padding: 4px; border-radius: var(--sm-radius); width: fit-content; }
  .segment-button { border: 1px solid transparent; border-radius: 9px; background: none; min-height: 44px; min-width: 62px; font-size: .9rem; font-weight: 670; }
  .segment-button[aria-pressed="true"] { background: white; border-color: var(--stroke); color: var(--purple-dark); }
  .spend-filters { display: flex; align-items: end; flex-wrap: wrap; gap: 8px; }
  .field { display: grid; align-content: start; gap: 4px; min-width: 0; font-size: .875rem; font-weight: 660; }
  .field input, .field textarea, dialog textarea {
    min-width: 0; max-width: 100%; min-height: 44px; border: 1px solid #bfb3ce; background: white; border-radius: 10px; padding: 8px 10px;
  }
  .field input, .field select, .field textarea { font-size: 1rem; }
  .field textarea, .pre-wrap { white-space: pre-wrap; overflow-wrap: anywhere; }
  .search-count { display: block; margin: 8px 0 0; }
  .wealth { background: #f1ecfa; border-color: #ded2f0; }
  .wealth-kpis { display: grid; gap: 6px; }
  .wealth-kpis div { display: flex; flex-wrap: wrap; gap: 3px 10px; justify-content: space-between; align-items: baseline; }
  .wealth-kpis span { color: var(--muted); }
  .wealth-kpis .money { font-size: clamp(1rem, 4.5vw, 1.55rem); letter-spacing: -.05em; }
  .plan-notes { padding-left: 21px; margin: 8px 0 0; }
  .plan-notes li + li { margin-top: 5px; }
  .boot, .lock { max-width: 520px; width: 100%; margin: 0 auto; }
  .lock { display: grid; gap: 8px; margin-top: clamp(12px, 7vh, 80px); padding: clamp(18px, 4vw, 32px); }
  .lock h2 { font-size: 1.6rem; }
  .lock-actions { display: flex; gap: 8px; flex-wrap: wrap; }
  .local-unlock { border-top: 1px solid var(--stroke); margin-top: 10px; padding-top: 10px; }
  dialog {
    width: min(calc(100% - 28px), 560px); max-height: min(88dvh, 850px); overflow: auto; padding: 17px;
    border: 1px solid var(--stroke); border-radius: var(--radius); box-shadow: 0 20px 100px #16102244; color: var(--ink); background: var(--paper);
  }
  dialog::backdrop { background: #20182cb5; }
  .dialog-title { display: flex; gap: 12px; justify-content: space-between; align-items: center; margin-bottom: 8px; }
  .dialog-title h2 { overflow-wrap: anywhere; min-width: 0; }
  .editor { display: grid; gap: 8px; margin-top: 11px; }
  .editor > button { justify-self: start; }
  .form-status { color: var(--copper); font-size: .87rem; }
  .form-status:empty { display: none; }
  .toast {
    position: fixed; z-index: 3; bottom: calc(75px + env(safe-area-inset-bottom, 0px)); inset-inline: 14px;
    margin-inline: auto; max-width: 520px; background: var(--rail); color: white;
    padding: 10px 14px; border-radius: var(--sm-radius); box-shadow: var(--shadow); font-size: .88rem;
  }
  .toast:empty { visibility: hidden; }
}

@layer responsive {
  @media (min-width: 600px) {
    .groups { grid-template-columns: repeat(3,minmax(0, 1fr)); gap: 13px; }
    .group { grid-template-columns: 1fr; gap: 6px; padding: 14px; }
    .group .detail-hint { text-align: left; }
    .group .money { justify-self: start; }
    .grid-two { grid-template-columns: repeat(2,minmax(0,1fr)); }
    .wealth-kpis { grid-template-columns: repeat(3,minmax(0,1fr)); gap: 12px; }
    .wealth-kpis div { display: grid; }
  }
  @media (min-width: 900px) {
    .layout { display: grid; grid-template-columns: 195px minmax(0,1fr); }
    .rail { position: sticky; top: 0; height: 100dvh; display: flex; flex-direction: column; padding: 22px 13px; gap: 22px; }
    .brand { padding: 3px 8px; }
    .nav { position: static; grid-template-columns: 1fr; padding: 0; gap: 2px; border: none; }
    .nav-item { flex-direction: row; justify-content: start; gap: 10px; min-height: 49px; padding: 5px 12px; font-size: .96rem; text-align: left; }
    .rail-state { display: block; margin-top: auto; margin-left: 9px; }
    .workspace { width: 100%; padding-bottom: 30px; }
    .grid-spend { grid-template-columns: minmax(0, .9fr) minmax(0, 1.3fr); }
    .toast { bottom: 22px; left: 200px; }
  }
  @media (min-width: 1180px) {
    .layout { grid-template-columns: 220px minmax(0,1fr); }
    .top { grid-template-columns: 1fr auto; align-items: start; }
    .tools { justify-content: flex-end; }
    .lead-card { display: grid; grid-template-columns: 1.05fr 1fr; gap: 18px; align-items: center; min-height: 164px; }
    .lead-metrics { padding-top: 0; }
    .lead-metric { padding: 9px 11px; }
  }
  @media (hover: hover) {
    .button:not(.primary):hover, .icon-button:not([disabled]):hover, .actionable:hover, .transaction:hover, .trend-month:hover { background-color: var(--lavender); }
    .button.primary:hover { background: var(--purple); }
    .nav-item:hover { background: #514263; }
    .lead-metric:hover { background: #fff3; }
    .group:hover { border-color: var(--accent); }
  }
  @media (prefers-reduced-motion: no-preference) {
    button, .button { transition: background-color .16s var(--ease-out), border-color .16s var(--ease-out); }
  }
}
