:root {
  --bg: #f7f7f8;
  --fg: #0f1115;
  --muted: #6b7280;
  --card: #ffffff;
  --line: #e5e7eb;
  --pos: #16a34a;
  --pos-bg: #dcfce7;
  --neg: #dc2626;
  --neg-bg: #fee2e2;
  --accent: #2563eb;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --fg: #e5e7eb;
    --muted: #9ca3af;
    --card: #181b22;
    --line: #2a2f3a;
    --pos: #34d399;
    --pos-bg: #064e3b;
    --neg: #f87171;
    --neg-bg: #4c1d1d;
    --accent: #60a5fa;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  background: none;
  border: 0;
}

input {
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
}

.icon {
  min-width: 44px;
  min-height: 44px;
  border-radius: 10px;
  font-size: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon:hover { background: var(--line); }

/* landing */
body.landing {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
body.landing main {
  max-width: 420px;
  text-align: center;
}
body.landing h1 {
  font-size: 40px;
  margin: 0 0 12px;
}
body.landing p {
  color: var(--muted);
  margin: 0 0 20px;
}
body.landing button#create {
  background: var(--accent);
  color: white;
  padding: 14px 22px;
  border-radius: 12px;
  font-size: 17px;
  font-weight: 600;
}
body.landing .hint { font-size: 14px; margin-top: 16px; }

/* app */
body.app {
  max-width: 720px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
  min-height: 100vh;
}

.top {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
  padding: 8px 0 0;
}

main#home {
  text-align: center;
  padding: 12px 0 32px;
}

.balance {
  font-size: clamp(72px, 22vw, 128px);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.02em;
  color: var(--pos);
  font-variant-numeric: tabular-nums;
}
.balance.neg { color: var(--neg); }
.balance-label {
  color: var(--muted);
  margin-top: 4px;
  margin-bottom: 20px;
  font-size: 14px;
  text-transform: lowercase;
}

.spend {
  display: flex;
  align-items: stretch;
  gap: 8px;
  margin: 0 0 24px;
}
.spend-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  width: 44px;
  flex: 0 0 44px;
  border-radius: 12px;
  background: var(--card);
  border: 1px solid var(--line);
}
.spend input {
  flex: 0 0 80px;
  min-width: 0;
  min-height: 48px;
  text-align: center;
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  -moz-appearance: textfield;
  appearance: textfield;
}
.spend input::-webkit-outer-spin-button,
.spend input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.spend button {
  flex: 1;
  min-height: 48px;
  background: var(--neg-bg);
  color: var(--neg);
  border-radius: 12px;
  font-weight: 600;
  font-size: 15px;
  padding: 0 12px;
}
.spend button:active { transform: scale(0.98); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
}

.cat {
  min-height: 76px;
  border-radius: 14px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: space-between;
  text-align: left;
  background: var(--card);
  border: 1px solid var(--line);
  transition: transform 80ms ease;
}
.cat:active { transform: scale(0.97); }
.cat-name {
  font-size: 14px;
  font-weight: 500;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
.cat-pts {
  font-weight: 700;
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  align-self: flex-end;
}
.cat.pos { background: var(--pos-bg); border-color: transparent; }
.cat.pos .cat-pts { color: var(--pos); }
.cat.neg { background: var(--neg-bg); border-color: transparent; }
.cat.neg .cat-pts { color: var(--neg); }

/* toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.2);
  z-index: 100;
}

/* modal */
.modal {
  border: 0;
  border-radius: 16px;
  background: var(--bg);
  color: var(--fg);
  padding: 0;
  width: min(92vw, 540px);
  max-height: 86vh;
  overflow: hidden;
}
.modal::backdrop { background: rgba(0,0,0,0.5); }
.modal header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 12px 20px;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--bg);
}
.modal h2 { margin: 0; font-size: 18px; }
.modal-body {
  padding: 16px 20px 24px;
  overflow-y: auto;
  max-height: calc(86vh - 56px);
}

/* history */
.day { margin-bottom: 20px; }
.day h3 {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: var(--muted);
  font-weight: 500;
  margin: 0 0 8px;
}
.day-total { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--fg); }
.day ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }

.entry {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.entry.deleted { opacity: 0.5; }
.entry.deleted .entry-name { text-decoration: line-through; }
.entry-name { font-size: 15px; }
.entry-time { font-size: 12px; color: var(--muted); margin-top: 2px; }
.entry-pts { font-weight: 700; font-variant-numeric: tabular-nums; }
.entry-pts.pos { color: var(--pos); }
.entry-pts.neg { color: var(--neg); }

/* settings */
.cat-editor {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.cat-row {
  display: grid;
  grid-template-columns: 1fr 72px auto;
  gap: 8px;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}
.cat-row:last-child { border-bottom: 0; }
.cat-row input { width: 100%; }
.cat-pts-input { text-align: right; font-variant-numeric: tabular-nums; }
@media (max-width: 480px) {
  .cat-row {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "name name"
      "pts del";
    row-gap: 6px;
  }
  .cat-name-input { grid-area: name; }
  .cat-pts-input { grid-area: pts; width: 96px; text-align: left; }
  .cat-row > button { grid-area: del; }
}

button.secondary {
  display: inline-flex;
  align-items: center;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px dashed var(--line);
  color: var(--muted);
  font-weight: 500;
}
button.secondary:hover { background: var(--card); color: var(--fg); }

.hint { color: var(--muted); font-size: 13px; margin-top: 12px; }
