
/* iOS-inspired stylesheet. System colors, SF font stack, grouped cards,
   fill-style inputs, blur topbar. Light + dark via prefers-color-scheme. */

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

:root {
  /* Light mode (iOS systemBackground / grouped). */
  --bg: #F2F2F7;
  --card: #FFFFFF;
  --card-2: #F2F2F7;
  --separator: rgba(60, 60, 67, 0.18);
  --text: #000000;
  --text-2: rgba(60, 60, 67, 0.60);
  --text-3: rgba(60, 60, 67, 0.30);
  --fill: rgba(120, 120, 128, 0.12);
  --fill-2: rgba(120, 120, 128, 0.20);
  --blue: #007AFF;
  --green: #34C759;
  --red: #FF3B30;
  --orange: #FF9500;
  --yellow: #FFCC00;
  --bar-bg: rgba(249, 249, 249, 0.82);
  --shadow: 0 1px 0 rgba(0,0,0,0.04);
  --r-lg: 14px;
  --r-md: 10px;
  --r-sm: 8px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1C1C1E;
    --card-2: #2C2C2E;
    --separator: rgba(84, 84, 88, 0.65);
    --text: #FFFFFF;
    --text-2: rgba(235, 235, 245, 0.60);
    --text-3: rgba(235, 235, 245, 0.30);
    --fill: rgba(118, 118, 128, 0.24);
    --fill-2: rgba(118, 118, 128, 0.36);
    --blue: #0A84FF;
    --green: #30D158;
    --red: #FF453A;
    --orange: #FF9F0A;
    --yellow: #FFD60A;
    --bar-bg: rgba(28, 28, 30, 0.82);
    --shadow: none;
  }
}

html, body { margin: 0; padding: 0; }
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
body {
  font: 17px/1.41 -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
        system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-top: env(safe-area-inset-top);
  padding-bottom: max(env(safe-area-inset-bottom), 16px);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  text-rendering: optimizeLegibility;
}
main { max-width: 640px; margin: 0 auto; padding: 0 16px 32px; }
a { color: var(--blue); text-decoration: none; }
a:active { opacity: 0.6; }

h1, h2, h3 { letter-spacing: -0.022em; }
h1 { font-size: 34px; font-weight: 700; margin: 0; line-height: 1.12; }
h2 { font-size: 22px; font-weight: 600; margin: 0; }
h3 { font-size: 17px; font-weight: 600; margin: 0; }

.muted, p.muted { color: var(--text-2); }
.dim { color: var(--text-3); }
small, .small { font-size: 13px; color: var(--text-2); }

/* ---------- Top bar (iOS large title + blur) ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--separator);
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  min-height: 44px;
}
.brand-link {
  font-weight: 700; font-size: 17px;
  text-decoration: none; color: var(--text);
  letter-spacing: -0.022em;
}
.nav-links { display: flex; gap: 18px; }
.nav-links a {
  color: var(--blue); text-decoration: none;
  font-size: 15px;
}

/* ---------- Cards / grouped sections ---------- */
.card {
  background: var(--card);
  border: none;
  border-radius: var(--r-lg);
  padding: 16px 18px;
  margin: 16px 0;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 16px; }
.card h2 { font-size: 20px; margin: 0 0 4px; }
.card > h2 + p { margin-top: 0; }
.card h3 { font-size: 16px; font-weight: 600; margin: 14px 0 6px; }
.card p { margin: 8px 0; }
.card p:first-child { margin-top: 0; }
.card p:last-child { margin-bottom: 0; }

.card.warn { background: rgba(255, 204, 0, 0.14); }
.card.danger h2 { color: var(--red); }

/* ---------- Forms ---------- */
label {
  display: block;
  font-size: 13px; font-weight: 500;
  color: var(--text-2);
  margin: 14px 0 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
input, textarea, select {
  width: 100%;
  font: inherit;
  font-size: 17px;
  color: var(--text);
  background: var(--fill);
  border: none;
  border-radius: var(--r-md);
  padding: 12px 14px;
  margin: 0;
  -webkit-appearance: none;
  appearance: none;
  transition: background-color 0.12s ease;
}
input:focus, textarea:focus, select:focus {
  outline: none;
  background: var(--fill-2);
  box-shadow: 0 0 0 2px var(--blue) inset;
}
textarea { resize: vertical; min-height: 80px; line-height: 1.4; }
input::placeholder, textarea::placeholder { color: var(--text-3); }

/* iOS-style search input with embedded magnifier */
input[type="search"] {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path fill='%238E8E93' d='M11 6.5a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0zm-.82 3.74a6 6 0 1 1 1.06-1.06l3.04 3.04a.75.75 0 1 1-1.06 1.06l-3.04-3.04z'/></svg>");
  background-repeat: no-repeat;
  background-position: 12px center;
  background-size: 16px 16px;
  padding-left: 38px;
}
input[type="checkbox"] {
  width: 22px; height: 22px; padding: 0;
  background: var(--fill);
  border-radius: 6px;
  display: inline-grid; place-content: center;
  cursor: pointer;
}
input[type="checkbox"]:checked {
  background: var(--blue);
  box-shadow: none;
}
input[type="checkbox"]:checked::after {
  content: ""; width: 10px; height: 6px;
  border: solid white; border-width: 0 0 2px 2px;
  transform: rotate(-45deg); margin-top: -2px;
}

/* iOS-style switch */
.switch {
  position: relative;
  display: inline-block;
  width: 51px; height: 31px;
  flex-shrink: 0;
}
.switch input[type="checkbox"] {
  /* Hide the native control without removing its hit area. */
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  opacity: 0;
  margin: 0; padding: 0;
  background: transparent;
  border-radius: 999px;
  cursor: pointer;
  display: block;
  z-index: 1;
}
.switch .track {
  position: absolute; inset: 0;
  background: rgba(120, 120, 128, 0.32);
  border-radius: 999px;
  transition: background-color 0.2s ease;
}
.switch .track::after {
  content: "";
  position: absolute;
  width: 27px; height: 27px;
  top: 2px; left: 2px;
  background: #FFFFFF;
  border-radius: 50%;
  box-shadow: 0 3px 8px rgba(0,0,0,0.15), 0 1px 1px rgba(0,0,0,0.16);
  transition: transform 0.2s ease;
}
.switch input[type="checkbox"]:checked ~ .track {
  background: var(--green);
}
.switch input[type="checkbox"]:checked ~ .track::after {
  transform: translateX(20px);
}
.switch input[type="checkbox"]:focus-visible ~ .track {
  box-shadow: 0 0 0 2px var(--blue);
}

.toggle-row {
  display: flex; align-items: center; gap: 16px;
  padding: 6px 0 4px;
  margin-top: 8px;
}
.toggle-label { flex: 1; font-size: 17px; line-height: 1.25; }
.sr-only {
  position: absolute; width: 1px; height: 1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap;
}

/* ---------- Buttons ---------- */
button, .button {
  display: inline-flex; align-items: center; justify-content: center;
  font: inherit; font-size: 17px; font-weight: 500;
  cursor: pointer;
  background: var(--fill);
  color: var(--blue);
  border: none;
  border-radius: var(--r-md);
  padding: 12px 18px;
  min-height: 44px;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.12s ease, background-color 0.12s ease;
}
button:active, .button:active { opacity: 0.65; }
button.primary, .button.primary {
  background: var(--blue);
  color: #FFFFFF;
  font-weight: 600;
  width: 100%;
  padding: 14px 18px;
  margin-top: 8px;
}
button.danger {
  background: var(--red);
  color: #FFFFFF;
  font-weight: 600;
  width: 100%;
  padding: 14px 18px;
}
button.link {
  background: transparent;
  color: var(--blue);
  padding: 8px 6px;
  min-height: auto;
  font-weight: 500;
}
.danger-link {
  background: transparent; border: none;
  color: var(--red);
  padding: 8px 0;
  min-height: auto;
  font: inherit; font-size: 15px;
  cursor: pointer;
}

/* ---------- Auth / landing ---------- */
.auth { padding-top: 40px; }
.brand h1 { font-size: 40px; letter-spacing: -0.03em; }
.brand p { margin: 6px 0 0; color: var(--text-2); font-size: 17px; }
.legal { text-align: center; margin-top: 28px; font-size: 13px; }

.key-display {
  display: block; word-break: break-all;
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: 14px;
  background: var(--fill);
  padding: 14px 16px;
  border-radius: var(--r-md);
  margin: 12px 0;
  line-height: 1.4;
  user-select: all;
}

/* ---------- Flash banner ---------- */
.flash {
  padding: 12px 16px;
  border-radius: var(--r-md);
  margin: 16px 0;
  font-size: 15px;
  font-weight: 500;
}
.flash.ok { background: rgba(52, 199, 89, 0.16); color: var(--green); }

/* ---------- Dashboard summary tiles ---------- */
.summary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin: 16px 0 4px;
}
.summary .stat {
  background: var(--card);
  border-radius: var(--r-md);
  padding: 12px 8px;
  text-align: center;
  color: var(--text);
  text-decoration: none;
  display: flex; flex-direction: column;
  min-height: 64px;
  justify-content: center;
  box-shadow: var(--shadow);
}
.summary .stat .n {
  font-size: 24px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.summary .stat .lbl {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-2);
  margin-top: 4px;
  letter-spacing: 0.01em;
}
.summary .stat.dim .n { color: var(--text-3); font-weight: 500; }
@media (max-width: 420px) {
  .summary { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Search / filter row ---------- */
.filter-form {
  display: flex; gap: 10px; align-items: stretch; flex-wrap: wrap;
  margin: 12px 0 14px;
}
.filter-form input[type="search"] {
  flex: 1 1 200px;
  min-width: 0;
}
.filter-form .button {
  padding: 0 18px;
  min-height: 44px;
  font-size: 15px;
  font-weight: 500;
  background: var(--fill);
}
.filter-chip {
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(0,122,255,0.12);
  color: var(--blue);
  font-weight: 500;
}
.filter-chip strong { font-weight: 700; margin-left: 4px; }

/* ---------- History section ---------- */
.history-head {
  display: flex; align-items: baseline; justify-content: space-between;
  padding: 12px 4px 4px;
  gap: 8px;
}
.history-head h2 { font-size: 22px; }
.count {
  color: var(--text-2);
  font-weight: 500;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  margin-left: 6px;
}
.events { list-style: none; padding: 0; margin: 4px 0 0; }
.day-sep {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 20px 4px 8px;
}
.day-sep:first-child { margin-top: 8px; }

/* ---------- Event card (iOS list cell) ---------- */
.event {
  background: var(--card);
  border: none;
  border-radius: var(--r-lg);
  padding: 14px 16px;
  margin: 0 0 10px;
  box-shadow: var(--shadow);
}
.event-head {
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px;
}
.event .when {
  color: var(--text-2);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}
.event .name {
  margin: 4px 0 0;
  font-size: 17px;
  font-weight: 600;
  line-height: 1.3;
  display: flex; align-items: baseline; flex-wrap: wrap; gap: 8px;
}
.event .qty {
  font-weight: 400;
  color: var(--text-2);
  font-size: 15px;
}
.event .qty::before { content: "\B7"; margin-right: 6px; color: var(--text-3); }
.event .tags {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin: 8px 0 0;
}
.event .tag {
  font-size: 13px;
  font-weight: 500;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(0,122,255,0.12);
  color: var(--blue);
  text-decoration: none;
  border: none;
}
.event .tag:active { opacity: 0.6; }
.event .notes {
  margin: 8px 0 0;
  white-space: pre-wrap;
  color: var(--text);
  font-size: 15px;
}
.event .src {
  font-size: 12px;
  margin: 8px 0 0;
  color: var(--text-3);
}
.event .row {
  display: flex; gap: 10px; align-items: center; margin-top: 12px;
}
.event .row .primary { width: auto; flex: 1 1 auto; margin-top: 0; }
.event .row .link { flex: 0 0 auto; }
.event-head .toggle-edit { font-size: 15px; padding: 6px 8px; margin: -6px -8px; }
.edit-form[hidden] { display: none; }
.edit-form { margin-top: 12px; padding-top: 12px; border-top: 0.5px solid var(--separator); }
.edit-form label { margin-top: 12px; }
.edit-form label:first-child { margin-top: 0; }
.del-form { margin-top: 4px; text-align: right; }

.empty { padding: 28px 12px; text-align: center; color: var(--text-2); }

/* ---------- Quick add ---------- */
.add-form details { margin-top: 12px; }
.add-form details summary {
  color: var(--blue);
  cursor: pointer;
  padding: 8px 0;
  font-size: 15px;
  font-weight: 500;
  list-style: none;
  user-select: none;
}
.add-form details summary::-webkit-details-marker { display: none; }
.add-form details summary::after { content: " \203A"; transition: transform 0.2s; display: inline-block; }
.add-form details[open] summary::after { transform: rotate(90deg); }

/* ---------- Settings extras ---------- */
.checkrow {
  display: flex; gap: 12px; align-items: flex-start;
  color: var(--text);
  margin: 12px 0;
  cursor: pointer;
  font-size: 15px;
  line-height: 1.4;
  padding: 4px 0;
}
.checkrow input { width: auto; flex-shrink: 0; margin-top: 1px; }
.rotate-form { display: flex; flex-direction: column; gap: 4px; }

/* ---------- Shortcut page ---------- */
.curl {
  background: var(--fill);
  border-radius: var(--r-md);
  padding: 14px 16px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.45;
}
.steps { padding-left: 22px; }
.steps li { margin-bottom: 10px; line-height: 1.45; }
.steps ul { margin: 6px 0; padding-left: 18px; }
.steps code, .curl code, code.copy {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  font-size: 14px;
}
code.copy {
  cursor: pointer;
  padding: 2px 6px;
  background: var(--fill);
  border-radius: 4px;
}
code.copy:active { background: var(--fill-2); }

.import-form textarea {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  /* Stay at 16px+ so iOS Safari doesn't auto-zoom on focus. */
  font-size: 16px;
  line-height: 1.45;
}

/* ---------- Nutrition summary cards ---------- */
.nutri-summary {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 12px 0 4px;
}
.nutri-card {
  background: var(--card);
  border-radius: var(--r-lg);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.ntitle {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 10px;
}
.ngrid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}
.ncell {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 4px 2px;
  text-align: center;
  min-width: 0;
}
.ncell .nl {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.02em;
  margin-bottom: 2px;
}
.ncell .nv {
  font-size: 17px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}
.ncell .nv.dim { color: var(--text-3); font-weight: 500; }
@media (max-width: 380px) {
  .ncell .nv { font-size: 15px; }
  .nutri-card { padding: 12px 12px; }
}

/* ---------- Nutrition pills on event cards ---------- */
.nutri-row {
  display: flex; flex-wrap: wrap;
  gap: 6px;
  margin: 8px 0 0;
}
.npill {
  display: inline-flex; align-items: baseline;
  gap: 4px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--fill);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.npill .nl {
  color: var(--text-2);
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.npill .nv {
  font-weight: 600;
}

/* ---------- Nutrition input grid (in add/edit forms) ---------- */
.nutri-input-group {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 0.5px solid var(--separator);
}
.nutri-input-heading {
  font-size: 13px; font-weight: 500;
  color: var(--text-2);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.nutri-input-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px;
}
.nutri-input {
  display: flex; flex-direction: column;
  gap: 4px;
  margin: 0;
  text-transform: none;
  letter-spacing: 0;
}
.nutri-input-label {
  font-size: 12px;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.02em;
  font-weight: 500;
}
.nutri-input input {
  padding: 10px 12px;
  font-variant-numeric: tabular-nums;
}

/* ---------- Responsive ---------- */
@media (max-width: 480px) {
  main { padding: 0 14px 32px; }
  .topbar { padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top)); }
  .nav-links { gap: 14px; }
  .nav-links a { font-size: 15px; }
}
