:root {
  color-scheme: light;
  --bg: #f1f2f6;
  --surface: #ffffff;
  --control: #f1f2f6;
  --ink: #17181c;
  --ink-2: #5d616b;
  --muted: #8a8e99;
  --line: #e7e8ee;
  --track: #ecedf2;
  --accent: #4f46e5;
  --accent-ink: #ffffff;
  --accent-soft: #ecebfd;
  --income: #15803d;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgb(23 24 28 / 0.04), 0 12px 32px rgb(23 24 28 / 0.06);
  --sheet-shadow: 0 -12px 40px rgb(23 24 28 / 0.12);

  --font: "Onest", -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  --r-hero: 28px;
  --r-panel: 22px;
  --r-key: 16px;
  --r-badge: 12px;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e0f13;
  --surface: #1a1b21;
  --control: #24252d;
  --ink: #f3f4f7;
  --ink-2: #b4b7c0;
  --muted: #7d818c;
  --line: #2a2b33;
  --track: #2a2b33;
  --accent: #8b84ff;
  --accent-ink: #0e0f13;
  --accent-soft: #29274a;
  --income: #34c759;
  --danger: #ff6b6b;
  --shadow: none;
  --sheet-shadow: 0 -12px 40px rgb(0 0 0 / 0.5);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: 0;
  background: none;
  padding: 0;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

svg {
  display: block;
}

h1,
h2 {
  margin: 0;
}

.num {
  font-variant-numeric: tabular-nums;
}

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: 8px 16px calc(112px + env(safe-area-inset-bottom, 0px));
  display: grid;
  gap: 12px;
  transition: opacity 0.2s;
}

.app.is-loading {
  opacity: 0.55;
}

/* Month switcher */

.month-bar {
  display: grid;
  grid-template-columns: 40px 40px 1fr 40px 40px;
  align-items: center;
  padding: 4px 0;
}

.month-bar h1 {
  text-align: center;
  font-size: 17px;
  font-weight: 600;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--ink-2);
}

.icon-btn:active {
  background: var(--control);
}

.icon-btn svg,
.add-btn svg,
.keypad svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Segmented control */

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  padding: 3px;
  border-radius: 14px;
  background: var(--control);
}

.segmented button {
  height: 34px;
  border-radius: 11px;
  font-weight: 500;
  color: var(--ink-2);
  transition: background 0.2s, color 0.2s;
}

.segmented button[aria-selected="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 3px rgb(23 24 28 / 0.1);
}

:root[data-theme="dark"] .segmented button[aria-selected="true"] {
  background: #33343e;
}

/* Hero: donut + legend */

.hero {
  background: var(--surface);
  border-radius: var(--r-hero);
  box-shadow: var(--shadow);
  padding: 16px 16px 8px;
  display: grid;
  gap: 8px;
}

.donut {
  position: relative;
  width: min(232px, 70vw);
  aspect-ratio: 1;
  margin: 8px auto 4px;
}

.donut svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.donut circle {
  fill: none;
  transition: stroke-dasharray 0.9s var(--ease), stroke-dashoffset 0.9s var(--ease),
    stroke-width 0.25s var(--ease), opacity 0.25s;
}

.donut .seg {
  cursor: pointer;
}

.donut svg.has-selection .seg:not(.is-selected) {
  opacity: 0.28;
}

.donut-center {
  position: absolute;
  inset: 22%;
  display: grid;
  place-content: center;
  text-align: center;
  pointer-events: none;
}

.donut-center span {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.25;
}

.donut-center strong {
  font-size: clamp(22px, 7.4vw, 30px);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.15;
  white-space: nowrap;
}

.donut-share {
  min-height: 1.25em;
}

.legend {
  list-style: none;
  margin: 0;
  padding: 0;
}

.legend li + li {
  border-top: 1px solid var(--line);
}

.legend button {
  width: 100%;
  display: grid;
  grid-template-columns: 40px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px 0;
  text-align: left;
  border-radius: 12px;
  transition: opacity 0.2s;
}

.legend.has-selection button:not(.is-selected) {
  opacity: 0.45;
}

.legend-main {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.legend-title {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-weight: 500;
}

.legend-title span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.legend-title span:last-child {
  color: var(--muted);
  font-weight: 400;
}

.share-bar {
  height: 4px;
  border-radius: 2px;
  background: var(--track);
  overflow: hidden;
}

.share-bar i {
  display: block;
  height: 100%;
  border-radius: 2px;
}

.legend-amount {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.empty {
  padding: 8px 0 12px;
  text-align: center;
  color: var(--muted);
}

/* Category badge */

.badge {
  width: 40px;
  height: 40px;
  border-radius: var(--r-badge);
  display: grid;
  place-items: center;
  font-size: 20px;
  line-height: 1;
}

/* Stats */

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat {
  background: var(--surface);
  border-radius: var(--r-panel);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  display: grid;
  gap: 2px;
}

.stat span {
  color: var(--muted);
  font-size: 13px;
}

.stat strong {
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
}

.is-income {
  color: var(--income);
}

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

/* Daily bars */

.panel {
  background: var(--surface);
  border-radius: var(--r-panel);
  box-shadow: var(--shadow);
  padding: 16px;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-bottom: 14px;
}

.panel-head h2,
.feed-head h2 {
  font-size: 17px;
  font-weight: 600;
}

.panel-head span {
  color: var(--muted);
  font-size: 13px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.bars {
  position: relative;
}

.bars-plot {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  align-items: end;
  height: 96px;
  gap: 2px;
  position: relative;
}

.bar {
  height: 100%;
  display: flex;
  align-items: flex-end;
}

.bar i {
  display: block;
  width: 100%;
  min-height: 3px;
  border-radius: 4px 4px 1px 1px;
  background: var(--accent);
  transition: height 0.6s var(--ease), background 0.2s;
}

.bar.is-zero i {
  background: var(--track);
}

.bar.is-future i {
  background: transparent;
}

.bars-plot.has-selection .bar:not(.is-selected) i {
  background: color-mix(in srgb, var(--accent) 30%, var(--track));
}

.bars-plot.has-selection .bar.is-zero:not(.is-selected) i {
  background: var(--track);
}

.bars-avg {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 1px dashed var(--muted);
  opacity: 0.6;
  pointer-events: none;
}

.bars-axis {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 2px;
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.bars-axis span {
  white-space: nowrap;
}

/* Feed */

.feed {
  display: grid;
  gap: 10px;
  margin-top: 4px;
}

.feed-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 0 4px;
  min-height: 32px;
}

.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border-radius: 16px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 500;
  font-size: 14px;
}

.day {
  background: var(--surface);
  border-radius: var(--r-panel);
  box-shadow: var(--shadow);
  padding: 4px 16px;
}

.day-head {
  display: flex;
  justify-content: space-between;
  padding: 10px 0 4px;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.day-head span:first-child::first-letter {
  text-transform: uppercase;
}

.tx {
  width: 100%;
  display: grid;
  grid-template-columns: 40px 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px 0;
  text-align: left;
}

.tx + .tx {
  border-top: 1px solid var(--line);
}

.tx:active {
  opacity: 0.6;
}

.tx-main {
  min-width: 0;
  display: grid;
}

.tx-main span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tx-main span:first-child {
  font-weight: 500;
}

.tx-main span + span {
  color: var(--muted);
  font-size: 13px;
}

.tx-amount {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.feed-empty {
  background: var(--surface);
  border-radius: var(--r-panel);
  padding: 28px 20px;
  text-align: center;
  color: var(--muted);
}

/* Add button */

.add-btn {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 52px;
  padding: 0 24px 0 20px;
  border-radius: 26px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  font-size: 16px;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 40%, transparent);
  transition: transform 0.15s var(--ease);
}

.add-btn:active {
  transform: translateX(-50%) scale(0.96);
}

/* Sheet */

.backdrop {
  position: fixed;
  inset: 0;
  background: rgb(10 11 14 / 0.42);
  opacity: 0;
  transition: opacity 0.25s;
}

.backdrop.is-open {
  opacity: 1;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 11;
  max-width: 520px;
  max-height: 92vh;
  margin: 0 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface);
  border-radius: 28px 28px 0 0;
  box-shadow: var(--sheet-shadow);
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  display: grid;
  gap: 14px;
  transform: translateY(100%);
  transition: transform 0.32s var(--ease);
}

.sheet.is-open {
  transform: translateY(0);
}

.sheet-tall {
  min-height: 70vh;
  align-content: start;
}

.sheet-title {
  font-size: 19px;
  font-weight: 700;
  text-align: center;
}

.sheet-grip {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  margin: 0 auto 2px;
}

.amount {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 6px;
  padding: 4px 0;
  font-size: 44px;
  font-weight: 700;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.amount.is-placeholder {
  color: var(--muted);
}

.amount-currency {
  color: var(--muted);
  font-weight: 500;
}

.amount.shake {
  animation: shake 0.36s;
}

@keyframes shake {
  20%,
  60% {
    transform: translateX(-6px);
  }
  40%,
  80% {
    transform: translateX(6px);
  }
}

.cat-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px 4px;
}

.cat-grid.shake {
  animation: shake 0.36s;
}

.cat {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 6px 2px;
  border-radius: 14px;
  font-size: 11.5px;
  line-height: 1.2;
  color: var(--ink-2);
  text-align: center;
}

.cat .badge {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  font-size: 22px;
  outline: 2px solid transparent;
  outline-offset: 2px;
  transition: outline-color 0.15s, transform 0.15s var(--ease);
}

.cat[aria-pressed="true"] {
  color: var(--ink);
  font-weight: 600;
}

.cat[aria-pressed="true"] .badge {
  outline-color: var(--cat-color);
  transform: scale(1.04);
}

.cat span:last-child {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.cat-add .badge {
  background: var(--control);
  color: var(--muted);
  font-size: 26px;
  font-weight: 400;
  border: 1.5px dashed var(--line);
}

.cat-preview {
  display: grid;
  justify-items: center;
  gap: 8px;
  font-size: 17px;
  font-weight: 600;
}

.cat-preview .is-placeholder {
  color: var(--muted);
  font-weight: 500;
}

.badge-lg {
  width: 72px;
  height: 72px;
  border-radius: 22px;
  font-size: 38px;
}

.field {
  display: grid;
  gap: 8px;
}

.field-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--muted);
  font-size: 13px;
}

.emoji-input {
  width: 72px;
  height: 32px;
  border: 0;
  border-radius: 10px;
  background: var(--control);
  text-align: center;
  outline: none;
}

.emoji-input:focus-visible {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  gap: 4px;
  max-height: 140px;
  overflow-y: auto;
}

.emoji-grid button {
  height: 40px;
  border-radius: 12px;
  font-size: 22px;
}

.emoji-grid button[aria-pressed="true"] {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 2px var(--accent);
}

.color-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
  justify-items: center;
}

.color-grid button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--swatch);
  outline: 2px solid transparent;
  outline-offset: 3px;
  transition: outline-color 0.15s;
}

.color-grid button[aria-pressed="true"] {
  outline-color: var(--swatch);
}

.note.shake {
  animation: shake 0.36s;
}

.date-chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.chip {
  flex: none;
  position: relative;
  display: inline-flex;
  align-items: center;
  height: 36px;
  padding: 0 14px;
  border-radius: 18px;
  background: var(--control);
  color: var(--ink-2);
  font-weight: 500;
  font-size: 14px;
  cursor: pointer;
}

.chip[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
}

.chip-date input {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  cursor: pointer;
}

.note {
  height: 44px;
  padding: 0 14px;
  border: 0;
  border-radius: 14px;
  background: var(--control);
  outline: none;
}

.note::placeholder {
  color: var(--muted);
}

.note:focus-visible {
  box-shadow: inset 0 0 0 2px var(--accent);
}

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

.keypad button {
  height: 52px;
  border-radius: var(--r-key);
  background: var(--control);
  font-size: 22px;
  font-weight: 500;
  display: grid;
  place-items: center;
  transition: transform 0.08s, background 0.15s;
}

.keypad button:active {
  transform: scale(0.95);
  background: var(--line);
}

.sheet-actions {
  display: flex;
  gap: 8px;
}

.btn {
  height: 54px;
  border-radius: 18px;
  font-weight: 600;
  font-size: 16px;
  padding: 0 20px;
}

.btn-primary {
  flex: 1;
  background: var(--accent);
  color: var(--accent-ink);
}

.btn-primary:disabled {
  opacity: 0.6;
}

.btn-danger {
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
}

/* Toast & blocked state */

.toast {
  position: fixed;
  left: 50%;
  top: calc(12px + env(safe-area-inset-top, 0px));
  transform: translateX(-50%);
  z-index: 20;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 14px;
  background: var(--ink);
  color: var(--bg);
  font-weight: 500;
  box-shadow: var(--shadow);
}

.toast.is-error {
  background: var(--danger);
  color: #fff;
}

.blocked {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 32px;
  text-align: center;
  color: var(--ink-2);
  background: var(--bg);
  z-index: 30;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}

#cats-add {
  position: sticky;
  bottom: 0;
  box-shadow: 0 -12px 16px var(--surface);
}

.btn-danger.is-armed {
  background: var(--danger);
  color: #fff;
}

.feed-hint,
.sheet-hint {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.feed-hint {
  padding: 0 4px;
}

.sheet-hint {
  text-align: center;
}

/* Category tiles & iPhone-style delete mode */

.cat-tile {
  position: relative;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

.cat-tile .cat {
  width: 100%;
}

.cat-remove {
  position: absolute;
  top: 0;
  left: 2px;
  z-index: 1;
  display: none;
  place-items: center;
  min-width: 22px;
  height: 22px;
  padding: 0;
  border-radius: 11px;
  background: var(--ink-2);
  color: var(--surface);
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
  box-shadow: 0 0 0 2px var(--surface);
  transition: background 0.15s;
}

.is-editing .cat-remove {
  display: grid;
}

.cat-tile.is-armed .cat-remove {
  padding: 0 8px;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
}

.is-editing .cat-tile-add {
  display: none;
}

.is-editing .cat-tile[data-id] .badge {
  animation: jiggle 0.26s ease-in-out infinite alternate;
}

.is-editing .cat-tile:nth-child(2n) .badge {
  animation-delay: -0.13s;
}

.is-editing .cat-tile:nth-child(3n) .badge {
  animation-duration: 0.3s;
}

@keyframes jiggle {
  from {
    transform: rotate(-2.5deg);
  }
  to {
    transform: rotate(2.5deg);
  }
}

.grid-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 28px;
  margin-bottom: -6px;
  color: var(--muted);
  font-size: 13px;
}

.link-btn {
  color: var(--accent);
  font-weight: 600;
  font-size: 15px;
  padding: 4px 0 4px 12px;
}

/* Period switch */

.period-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: -4px;
}

.period-row .segmented {
  flex: 1;
}

.today-chip {
  flex: none;
  height: 40px;
  padding: 0 14px;
  border-radius: 14px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  font-size: 14px;
}

/* Long press on operations */

.tx {
  transition: transform 0.2s var(--ease), opacity 0.2s;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

.tx.is-pressing {
  transform: scale(0.97);
  opacity: 0.7;
}

.tx-preview {
  background: var(--control);
  border-radius: 18px;
  padding: 4px 14px 2px;
}

.tx-preview .tx {
  cursor: default;
  color: inherit;
}

.tx-preview .tx:disabled {
  opacity: 1;
}

.tx-preview-date {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.tx-preview-date::first-letter {
  text-transform: uppercase;
}

.action-list {
  display: grid;
  gap: 8px;
}

.action-btn {
  height: 52px;
  border-radius: 16px;
  background: var(--control);
  font-weight: 600;
  font-size: 16px;
}

.action-btn.action-danger {
  color: var(--danger);
}

.action-btn.action-danger.is-armed {
  background: var(--danger);
  color: #fff;
}
