:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #16202a;
  --muted: #5d6b79;
  --border: #dde2e8;
  --accent: #0f766e;
  --accent-soft: #d5f0ec;
  --low: #d92d20;
  --low-soft: #fde7e5;
  --ok: #16a34a;
  --ok-soft: #dcf5e4;
  --high: #d97706;
  --high-soft: #fdefd8;
  --vhigh: #b54708;
  --band: rgba(22, 163, 74, 0.08);
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1419;
    --surface: #161e25;
    --surface-2: #1d2730;
    --text: #e6edf3;
    --muted: #93a1ae;
    --border: #2a3641;
    --accent: #2dd4bf;
    --accent-soft: #11332f;
    --low: #f97066;
    --low-soft: #3a1714;
    --ok: #4ade80;
    --ok-soft: #12301d;
    --high: #fbbf24;
    --high-soft: #35270c;
    --vhigh: #fb923c;
    --band: rgba(74, 222, 128, 0.08);
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
a { color: var(--accent); }
small, .muted { color: var(--muted); }
.tabular { font-variant-numeric: tabular-nums; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; gap: 8px; align-items: center; font-weight: 700; }
.brand img { border-radius: 7px; }
.conn { font-size: .8rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.conn::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.conn.ok::before { background: var(--ok); }
.conn.err::before { background: var(--low); }

main { max-width: 860px; margin: 0 auto; padding: 16px; }

.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: .72rem; color: var(--muted); text-decoration: none; padding: 4px 0;
}
.tabs a span { font-size: 1.25rem; line-height: 1; filter: grayscale(1); opacity: .75; }
.tabs a.active { color: var(--accent); font-weight: 600; }
.tabs a.active span { filter: none; opacity: 1; }
.tab-add {
  justify-self: center; width: 54px; height: 54px; margin-top: -22px;
  border-radius: 50%; border: 0; background: var(--accent); color: #fff;
  font-size: 2rem; line-height: 1; box-shadow: 0 4px 12px rgba(15, 118, 110, .35); cursor: pointer;
}

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px;
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }

/* Glucosa actual */
.now { display: flex; align-items: center; gap: 18px; }
.now-value {
  min-width: 132px; padding: 14px 12px; border-radius: 16px; text-align: center;
  background: var(--surface-2);
}
.now-value .v { font-size: 3.2rem; font-weight: 750; line-height: 1; font-variant-numeric: tabular-nums; }
.now-value .u { font-size: .8rem; color: var(--muted); }
.now-value.low { background: var(--low-soft); color: var(--low); }
.now-value.ok { background: var(--ok-soft); color: var(--ok); }
.now-value.high { background: var(--high-soft); color: var(--high); }
.now-value.vhigh { background: var(--high-soft); color: var(--vhigh); }
.now-arrow { font-size: 2.4rem; line-height: 1; }
.now-meta { display: grid; gap: 4px; font-size: .92rem; }
.now-meta b { font-variant-numeric: tabular-nums; }
.stale { color: var(--low); font-weight: 600; }

.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.quick button {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 14px 8px; border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; box-shadow: var(--shadow);
}
.quick button span { font-size: 1.6rem; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 10px; }
.tile { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.tile .k { font-size: .75rem; color: var(--muted); }
.tile .v { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile .h { font-size: .72rem; color: var(--muted); }
.tile.good .v { color: var(--ok); }
.tile.warn .v { color: var(--high); }
.tile.bad .v { color: var(--low); }

.tir-bar { display: flex; height: 22px; border-radius: 6px; overflow: hidden; margin: 8px 0 6px; }
.tir-bar div { min-width: 0; }
.tir-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: .78rem; color: var(--muted); }
.tir-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-size: .82rem;
  padding: 5px 10px; border-radius: 8px; cursor: pointer;
}
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

.chart-box { position: relative; height: 260px; }
.chart-box.tall { height: 300px; }

/* Listas de registros */
.entries { list-style: none; margin: 0; padding: 0; }
.entry {
  display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center;
  padding: 10px 0; border-bottom: 1px solid var(--border); cursor: pointer;
}
.entry:last-child { border-bottom: 0; }
.entry .ic {
  width: 44px; height: 44px; border-radius: 10px; background: var(--surface-2);
  display: grid; place-items: center; font-size: 1.3rem; overflow: hidden;
}
.entry .ic img { width: 100%; height: 100%; object-fit: cover; }
.entry .t { font-weight: 600; }
.entry .s { font-size: .82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.entry .r { text-align: right; font-variant-numeric: tabular-nums; }
.entry .r b { display: block; }
.empty { color: var(--muted); text-align: center; padding: 18px 0; }

/* Formularios */
label { display: block; font-size: .85rem; font-weight: 600; margin: 12px 0 4px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
textarea { min-height: 70px; resize: vertical; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.row > *, .row3 > * { min-width: 0; }
input { min-width: 0; }
input[type="datetime-local"] { font-size: .92rem; padding-left: 8px; padding-right: 8px; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.check input { width: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 11px 16px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--low); border-color: var(--low); background: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.btn.small { padding: 6px 10px; font-size: .85rem; }
.actions { display: flex; gap: 10px; margin-top: 16px; }
.actions .btn { flex: 1; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 12px; border-radius: 999px; font: inherit; font-size: .9rem; cursor: pointer;
}
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.units-input { display: grid; grid-template-columns: 52px 1fr 52px; gap: 8px; align-items: center; }
.units-input input { text-align: center; font-size: 1.6rem; font-weight: 700; padding: 8px; }
.units-input button { height: 52px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 1.4rem; cursor: pointer; }

/* Hoja inferior (dialog) */
.sheet {
  width: min(560px, 100%); max-height: 92vh; margin: auto auto 0; padding: 0;
  border: 0; border-radius: 18px 18px 0 0; background: var(--surface); color: var(--text);
}
@media (min-width: 700px) { .sheet { margin: auto; border-radius: 18px; } }
.sheet::backdrop { background: rgba(0, 0, 0, .45); }
.sheet > div { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); overflow-y: auto; max-height: 92vh; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.sheet-head h2 { margin: 0; }
.x { border: 0; background: var(--surface-2); color: var(--text); width: 34px; height: 34px; border-radius: 50%; font-size: 1.1rem; cursor: pointer; }

.menu-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 10px; }
.menu-grid button {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left;
  padding: 14px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); font: inherit; cursor: pointer;
}
.menu-grid button span { font-size: 1.5rem; }
.menu-grid button small { font-size: .78rem; }

/* Foto de comida */
.photo-drop {
  display: grid; place-items: center; gap: 6px; min-height: 170px; border-radius: 14px;
  border: 2px dashed var(--border); background: var(--surface-2); color: var(--muted);
  text-align: center; cursor: pointer; overflow: hidden; position: relative;
}
.photo-drop img { width: 100%; max-height: 280px; object-fit: cover; display: block; }
.photo-drop { min-height: 120px; cursor: default; }
.photo-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.photo-btns .btn { padding: 13px 10px; }
.ai-result { margin-top: 12px; background: var(--surface-2); border-radius: 12px; padding: 12px; }
.ai-items { width: 100%; border-collapse: collapse; font-size: .9rem; }
.ai-items td { padding: 5px 4px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.ai-items td:last-child { width: 86px; }
.ai-items input { padding: 6px 8px; text-align: right; }
.ai-total { display: flex; justify-content: space-between; align-items: baseline; margin-top: 10px; }
.ai-total b { font-size: 1.6rem; }
.badge { display: inline-block; font-size: .72rem; padding: 2px 7px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); color: var(--muted); }
.badge.alta { color: var(--ok); border-color: var(--ok); }
.badge.baja { color: var(--high); border-color: var(--high); }
.note { font-size: .85rem; color: var(--muted); margin: 8px 0 0; }
.warn-box { background: var(--high-soft); color: var(--text); border-radius: 10px; padding: 10px 12px; font-size: .88rem; margin-top: 10px; }
.err-box { background: var(--low-soft); color: var(--text); border-radius: 10px; padding: 10px 12px; font-size: .88rem; margin-top: 10px; }

/* Tablas */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: .85rem; font-variant-numeric: tabular-nums; }
table.data th, table.data td { padding: 7px 8px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { font-size: .75rem; color: var(--muted); font-weight: 600; }

/* Informe IA */
.pattern { border-left: 4px solid var(--border); padding: 8px 12px; margin: 10px 0; background: var(--surface-2); border-radius: 0 10px 10px 0; }
.pattern.alta { border-color: var(--low); }
.pattern.media { border-color: var(--high); }
.pattern.baja { border-color: var(--ok); }
.pattern .ev { font-size: .85rem; color: var(--muted); margin-top: 4px; }

.login { max-width: 360px; margin: 12vh auto 0; }

.toast {
  position: fixed; left: 50%; bottom: calc(92px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100% - 32px); z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 420px) {
  .now { gap: 12px; }
  .now-value { min-width: 112px; }
  .now-value .v { font-size: 2.7rem; }
}
