:root {
  color-scheme: light dark;
  --bg: #f4f4f4;
  --surface: #ffffff;
  --header: #d9d9d9;
  --text: #1e1e1e;
  --muted: #666666;
  --line: #e0e0e0;
  --accent: #4f2d8f;
  --on-accent: #ffffff;
  --danger: #b3261e;
  --tile: #38344c;
  --tile-alt: #6c6482;
  --on-tile: #ffffff;
  --icon-filter: none;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tools-height: calc(56px + var(--safe-bottom));
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141414;
    --surface: #1f1f1f;
    --header: #2b2b2b;
    --text: #f1f1f1;
    --muted: #ababab;
    --line: #363636;
    --accent: #bda6ec;
    --on-accent: #1b1230;
    --danger: #ff8a80;
    --tile: #38344c;
    --tile-alt: #5a5370;
    --icon-filter: brightness(2.2);
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body.counting { overflow: hidden; overscroll-behavior: none; }

h1, h2, p, ul { margin: 0; padding: 0; }
ul { list-style: none; }
a { color: inherit; text-decoration: none; }

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  padding-bottom: var(--tools-height);
}

/* ---------- Estructura ---------- */

.top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: calc(60px + var(--safe-top));
  padding: calc(8px + var(--safe-top)) 16px 8px;
  background: var(--header);
}

.top h1 { font-size: 1.1875rem; font-weight: 600; line-height: 1.25; overflow-wrap: anywhere; }

.today {
  flex-shrink: 0;
  margin-left: auto;
  color: var(--accent);
  font-weight: 700;
  white-space: nowrap;
}

.today::first-letter { text-transform: uppercase; }

.back {
  display: grid;
  place-items: center;
  flex: none;
  width: 44px;
  height: 44px;
  margin-left: -10px;
  font-size: 1.75rem;
  line-height: 1;
}

.scroll {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px 16px 20px;
}

.dock {
  position: sticky;
  bottom: var(--tools-height);
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.muted { color: var(--muted); }
.small-text { font-size: .875rem; }
.section-title { margin-top: 10px; font-size: 1.0625rem; font-weight: 700; }
.stack { display: flex; flex-direction: column; gap: 8px; }

/* ---------- Botones ---------- */

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 52px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  touch-action: manipulation;
}

.btn.primary { border-color: var(--accent); background: var(--accent); color: var(--on-accent); }
.btn.danger { border-color: var(--danger); background: var(--danger); color: var(--bg); }
.btn.small { min-height: 44px; flex: none; }
.btn.icon { min-width: 48px; padding: 0; font-size: 1.25rem; }
.btn:disabled { opacity: .45; cursor: default; }

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

.link {
  min-height: 44px;
  padding: 0 4px;
  border: 0;
  background: none;
  font: inherit;
  font-size: .9375rem;
  text-decoration: underline;
  cursor: pointer;
}

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

/* ---------- Inicio ---------- */

.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  font-size: .9375rem;
}

.banner p { flex: 1; }

.list, .types {
  display: flex;
  flex-direction: column;
  margin: 0 -16px;
  border-block: 1px solid var(--line);
  background: var(--surface);
}

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

.card {
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 68px;
  padding: 10px 16px;
}

.card-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.card-main .muted { font-size: .875rem; }
.card-main em { font-style: normal; font-weight: 600; color: var(--text); }
.card-name { font-weight: 600; overflow-wrap: anywhere; }
.card-total { font-size: 1.375rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.resume { flex-direction: column; gap: 0; }
.resume-detail {
  max-width: 100%;
  font-size: .875rem;
  font-weight: 400;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.type {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  min-height: 72px;
  padding: 10px 16px;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
}

.type img, .type svg { flex: none; width: 44px; height: 44px; }
.type img, .card-icon { filter: var(--icon-filter); }
.type svg { color: var(--muted); }
.type span { flex: 1; }
.type::after { content: "›"; color: var(--muted); font-size: 1.5rem; line-height: 1; }

.card-icon { flex: none; width: 36px; height: 36px; }

/* ---------- Formularios ---------- */

.form { display: flex; flex-direction: column; gap: 20px; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.field .muted { font-weight: 400; }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; overflow-wrap: anywhere; }
.field.inline input { flex: none; width: 110px; text-align: right; }

input, textarea {
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  font-weight: 400;
}

textarea { resize: vertical; }

.add-row { display: flex; gap: 8px; }
.add-row input { flex: 1; min-width: 0; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips:empty { display: none; }

.chip {
  min-height: 44px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .9375rem;
  cursor: pointer;
}

.chip.on { border-color: var(--accent); background: var(--accent); color: var(--on-accent); font-weight: 600; }

.tabs { display: flex; border-bottom: 1px solid var(--line); }

.tab {
  flex: 1;
  min-height: 44px;
  padding: 8px 4px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: .8125rem;
  font-weight: 600;
  letter-spacing: .03em;
  text-transform: uppercase;
  cursor: pointer;
}

.tab.on { border-bottom-color: var(--accent); color: var(--accent); }

/* ---------- Pantalla de conteo ---------- */

.count {
  position: fixed;
  inset: 0 0 var(--tools-height);
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 8px;
  padding: 0 env(safe-area-inset-right, 0px) 8px env(safe-area-inset-left, 0px);
  background: var(--bg);
  touch-action: manipulation;
}

.count-top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: calc(56px + var(--safe-top));
  padding: var(--safe-top) 12px 0 6px;
  background: var(--header);
}

.count-top .back { margin-left: 0; }
.count-title { flex: 1; min-width: 0; }
.count-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.count-total { flex: none; font-size: 1rem; }
.count-total strong { font-size: 1.875rem; font-variant-numeric: tabular-nums; }

.grid {
  display: grid;
  grid-template-columns: 1fr;
  grid-auto-rows: minmax(0, 1fr);
  gap: 3px;
  min-height: 0;
  margin: 0 8px;
}

.grid[data-n="4"], .grid[data-n="5"], .grid[data-n="6"], .grid[data-n="7"], .grid[data-n="8"] {
  grid-template-columns: 1fr 1fr;
}

.grid[data-n="5"] .tile:last-child, .grid[data-n="7"] .tile:last-child { grid-column: 1 / -1; }

@media (orientation: landscape) and (max-height: 520px) {
  .grid[data-n="2"] { grid-template-columns: repeat(2, 1fr); }
  .grid[data-n="3"] { grid-template-columns: repeat(3, 1fr); }
  .grid[data-n="4"], .grid[data-n="5"], .grid[data-n="6"], .grid[data-n="7"], .grid[data-n="8"] { grid-template-columns: repeat(4, 1fr); }
  .grid[data-n="5"] .tile:last-child, .grid[data-n="7"] .tile:last-child { grid-column: auto; }
}

.tile {
  container-type: size;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  padding: 6px;
  border: 0;
  border-radius: 2px;
  background: var(--tile);
  color: var(--on-tile);
  font: inherit;
  cursor: pointer;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.tile:nth-child(even) { background: var(--tile-alt); }
.tile.pressed { filter: brightness(1.25); }

.tile-name {
  max-width: 100%;
  font-size: clamp(14px, 11cqh, 24px);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tile-count {
  font-size: 15vw;
  font-size: min(58cqh, 28cqw);
  font-weight: 800;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}

.tile-hint {
  display: none;
  max-width: 100%;
  margin-top: 2px;
  font-size: clamp(13px, 8cqh, 18px);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.grid.untouched .tile-hint { display: block; }

.bar { display: flex; gap: 8px; padding: 0 8px; }
.bar .btn { flex: 1 1 0; min-width: 0; padding: 8px 2px; }

/* ---------- Resumen ---------- */

.big-total {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 0 -16px;
  padding: 18px 12px;
  border-block: 1px solid var(--line);
  background: var(--surface);
}

.big-total .muted { font-weight: 600; }
.big-total strong { color: var(--accent); font-size: 4rem; line-height: 1.1; font-variant-numeric: tabular-nums; }

.rows { margin: 0 -16px; border-block: 1px solid var(--line); background: var(--surface); overflow: hidden; }

.rows li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 52px;
  padding: 8px 16px;
  overflow-wrap: anywhere;
}

.rows li + li { border-top: 1px solid var(--line); }
.rows strong { font-variant-numeric: tabular-nums; }
.row-end { display: flex; align-items: center; gap: 12px; flex: none; }

/* ---------- Barra inferior ---------- */

#tools {
  position: fixed;
  z-index: 5;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  justify-content: center;
  height: var(--tools-height);
  padding-bottom: var(--safe-bottom);
  background: var(--header);
}

.tool {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  max-width: 320px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: .75rem;
  font-weight: 500;
  cursor: pointer;
  touch-action: manipulation;
}

.tool svg { width: 24px; height: 24px; color: var(--muted); }

@media (orientation: landscape) and (max-height: 520px) {
  :root { --tools-height: calc(44px + var(--safe-bottom)); }
  .tool { flex-direction: row; gap: 8px; }
}

/* ---------- Diálogos y avisos ---------- */

dialog {
  width: min(100vw - 24px, 520px);
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}

dialog::backdrop { background: rgb(0 0 0 / .55); }

.dlg-body { display: flex; flex-direction: column; gap: 14px; padding: 18px 16px; }
.dlg-body h2 { font-size: 1.25rem; outline: none; }
.dlg-body input, .dlg-body textarea { background: var(--bg); }
.dlg-body .btn { background: var(--bg); }
.dlg-body .btn.primary { background: var(--accent); }
.dlg-body .btn.danger { background: var(--danger); }
.dlg-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.share-preview {
  max-height: 40vh;
  margin: 0;
  padding: 12px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--bg);
  font: inherit;
  font-size: .9375rem;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.dlg-x {
  position: sticky;
  top: 8px;
  z-index: 1;
  align-self: flex-end;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin: -10px -8px -48px 0;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}
.dlg-x svg { width: 24px; height: 24px; }
.dlg-x + h2 { padding-right: 40px; }

.about-line { text-align: center; }
.about-line .link { color: var(--muted); }
.about-section { display: flex; flex-direction: column; gap: 6px; }
.about-section h3 { font-size: 1.0625rem; font-weight: 700; }
.about-section p { font-size: .9375rem; line-height: 1.45; overflow-wrap: anywhere; }
.about-updated { font-size: .875rem; }

.dlg-note { color: var(--danger); font-size: .9375rem; }
.dlg-info { font-size: .9375rem; font-weight: 600; }
.dlg-note:empty, .dlg-info:empty { display: none; }
.btn.toggle { justify-content: space-between; gap: 12px; }
.btn.toggle[aria-pressed="true"] { border-color: var(--text); }

.edit-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.edit-cat { flex: 1 1 100%; }
.stepper { display: flex; flex: 1; gap: 6px; min-width: 0; }
.stepper input { flex: 1; min-width: 56px; text-align: center; }
.stepper .btn { flex: none; width: 48px; min-height: 48px; }

#toast {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: calc(84px + var(--tools-height));
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 4px;
  background: #2b2b2b;
  color: #fff;
  font-size: .9375rem;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%);
  transition: opacity .2s;
}

#toast.show { opacity: 1; }
