:root {
  --bg: #f6f2ea;
  --surface: #ffffff;
  --surface-2: #fbf9f5;
  --ink: #1c2230;
  --muted: #6a7180;
  --faint: #a2a7b1;
  --line: #e6dfd2;
  --accent: #1f4fa3;
  --accent-ink: #ffffff;
  --accent-soft: #e5ecf8;
  --ok: #1f7a4d;
  --ok-soft: #e2f3e9;
  --bad: #b3372b;
  --bad-soft: #f9e5e2;
  --warn-soft: #fff4d6;
  --shadow: 0 1px 2px rgb(28 34 48 / 6%), 0 4px 16px rgb(28 34 48 / 6%);
  --radius: 14px;
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151b;
    --surface: #1b2029;
    --surface-2: #171b23;
    --ink: #e9ebef;
    --muted: #a3a9b5;
    --faint: #6d7480;
    --line: #2b313c;
    --accent: #7ea6ff;
    --accent-ink: #0d1424;
    --accent-soft: #1f2a40;
    --ok: #5fcf95;
    --ok-soft: #173327;
    --bad: #ff8a7d;
    --bad-soft: #3a1f1c;
    --warn-soft: #3a3218;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12151b; --surface: #1b2029; --surface-2: #171b23; --ink: #e9ebef; --muted: #a3a9b5; --faint: #6d7480;
  --line: #2b313c; --accent: #7ea6ff; --accent-ink: #0d1424; --accent-soft: #1f2a40; --ok: #5fcf95; --ok-soft: #173327;
  --bad: #ff8a7d; --bad-soft: #3a1f1c; --warn-soft: #3a3218; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* Header */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; }
.brand-mark { width: 36px; height: 36px; border-radius: 10px; display: block; }
.brand-title { font-weight: 700; letter-spacing: .2px; }
.brand-sub { font-size: 12px; color: var(--muted); }
.net { font-size: 12px; padding: 3px 8px; border-radius: 99px; background: var(--warn-soft); color: var(--ink); }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border-radius: 12px;
  border: 0; background: transparent; color: var(--muted);
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }

main { max-width: 760px; margin: 0 auto; padding: 0 16px 24px; }
.view { padding-top: 12px; }

/* Search inputs */
.sticky-tools {
  position: sticky; top: calc(61px + env(safe-area-inset-top)); z-index: 10;
  background: var(--bg); padding: 8px 0 8px;
}
.search {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 0 12px; color: var(--muted);
}
.search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; outline: 0; padding: 11px 0; color: var(--ink); font-size: 16px; }
.search.big input { padding: 14px 0; font-size: 17px; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 10px 0 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--surface);
  border-radius: 99px; padding: 6px 12px; font-size: 14px; color: var(--muted); white-space: nowrap;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.count { font-size: 13px; color: var(--muted); padding: 6px 2px; }

/* Dictionary */
.dict-wrap { position: relative; }
.dict-list { padding-right: 22px; }
.letter {
  position: sticky; top: calc(61px + 106px + env(safe-area-inset-top)); z-index: 5;
  font-weight: 700; color: var(--accent); background: var(--bg);
  padding: 10px 2px 4px; font-size: 14px; letter-spacing: .5px;
}
.entry {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; align-items: start;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 6px 10px 14px; margin: 6px 0;
}
.entry .pt { font-weight: 600; font-size: 17px; }
.art { color: var(--faint); font-weight: 400; }
.entry .ru { grid-column: 1; color: var(--ink); }
.entry .en { grid-column: 1; color: var(--muted); font-size: 13px; }
.entry .speak { grid-row: 1 / span 3; grid-column: 2; align-self: center; }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); vertical-align: 2px; margin-left: 6px; font-weight: 500; }
.letters {
  position: absolute; right: -6px; top: 0; bottom: 0; width: 22px;
}
.letters-inner {
  position: sticky; top: calc(61px + 120px + env(safe-area-inset-top));
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  /* столбик букв всегда помещается между шапкой и нижним меню */
  height: min(640px, calc(100dvh - 61px - 120px - var(--tabs-h) - 44px - env(safe-area-inset-top) - env(safe-area-inset-bottom)));
}
.letters button {
  flex: 1; min-height: 0; border: 0; background: transparent; color: var(--accent);
  font-size: clamp(9px, 1.8dvh, 12px); font-weight: 600; padding: 0 4px; line-height: 1;
}
mark { background: color-mix(in srgb, var(--accent) 22%, transparent); color: inherit; border-radius: 3px; padding: 0 1px; }

/* Search results */
.group-title { font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 18px 2px 6px; }
.dish {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin: 8px 0; box-shadow: var(--shadow);
}
.dish-head { display: flex; gap: 10px; align-items: baseline; }
.dish .pt { font-weight: 650; font-size: 17px; flex: 1; }
.dish .price { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.dish .ru { margin-top: 2px; }
.dish .en { color: var(--muted); font-size: 13px; }
.where { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.where button, .where span {
  font-size: 12px; border-radius: 8px; padding: 3px 8px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--muted);
}
.where button { cursor: pointer; }
.breakdown { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.bd {
  border-radius: 10px; padding: 5px 9px; background: var(--surface-2); border: 1px dashed var(--line); font-size: 14px;
}
.bd b { font-weight: 600; }
.bd small { display: block; color: var(--muted); font-size: 12px; }
.bd.unknown { opacity: .6; }
.bd.minor { background: transparent; border-color: transparent; padding-left: 2px; padding-right: 2px; }
.empty { text-align: center; color: var(--muted); padding: 40px 12px; }
.empty .big { font-size: 40px; display: block; margin-bottom: 6px; }
.toggle-bd { margin-top: 8px; border: 0; background: none; color: var(--accent); padding: 0; font-size: 13px; }

/* Cards */
.cards-tools { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; padding-top: 4px; }
.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 7px 12px; border-radius: 9px; color: var(--muted); font-weight: 600; font-size: 14px; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
select {
  flex: 1; min-width: 160px; padding: 9px 10px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink);
}
.card-stage { position: relative; perspective: 1200px; margin: 10px 0 12px; }
.card-nav {
  position: absolute; top: 50%; z-index: 3; transform: translateY(-50%);
  width: 40px; height: 72px; border: 0; border-radius: 12px; background: transparent;
  color: var(--muted); font-size: 34px; line-height: 1; display: grid; place-items: center;
}
.card-nav.prev { left: 2px; }
.card-nav.next { right: 2px; }
.card-nav:hover:not(:disabled) { background: var(--accent-soft); color: var(--accent); }
.card-nav:disabled { opacity: .25; cursor: default; }
.flashcard {
  position: relative; display: block; width: 100%; height: min(40vh, 300px);
  border: 0; padding: 0; background: transparent; transform-style: preserve-3d;
  transition: transform .45s cubic-bezier(.2,.7,.2,1);
}
.flashcard.flipped { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow);
  padding: 22px 44px; text-align: center;
}
.face.back { transform: rotateY(180deg); background: linear-gradient(180deg, var(--surface), var(--surface-2)); }
.card-label { position: absolute; top: 14px; left: 0; right: 0; font-size: 12px; color: var(--muted); letter-spacing: .5px; text-transform: uppercase; }
.card-word { font-size: clamp(24px, 7vw, 36px); font-weight: 700; line-height: 1.2; overflow-wrap: anywhere; }
.face.back .card-word { color: var(--accent); }
.card-hint { position: absolute; bottom: 14px; font-size: 12px; color: var(--faint); }
.card-extra { color: var(--muted); font-size: 14px; max-width: 46ch; }
.card-extra .en { display: block; margin-top: 2px; }
.card-extra .src { display: block; margin-top: 8px; font-size: 12px; color: var(--faint); }
.answer { display: flex; gap: 8px; margin-bottom: 10px; }
.answer input {
  flex: 1; min-width: 0; padding: 11px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-size: 17px;
}
.verdict { border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; font-size: 15px; }
.verdict.ok { background: var(--ok-soft); color: var(--ok); }
.verdict.close { background: var(--warn-soft); }
.verdict.bad { background: var(--bad-soft); color: var(--bad); }
.card-actions { display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px; align-items: center; }
#card-speak { color: var(--accent); }
#card-speak.muted { color: var(--faint); }

/* Buttons */
.btn {
  border: 0; border-radius: 12px; padding: 11px 16px; font-weight: 650;
  background: var(--accent); color: var(--accent-ink);
}
.btn:disabled { opacity: .5; cursor: default; }
.btn.secondary { background: var(--accent-soft); color: var(--accent); }
.btn.ghost { background: transparent; color: var(--accent); border: 1px solid var(--line); }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn-no { background: var(--bad-soft); color: var(--bad); }
.btn-yes { background: var(--ok-soft); color: var(--ok); }
.btn.small { padding: 7px 11px; font-size: 14px; border-radius: 10px; }

/* Menus */
.menus-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.menus-head h2 { margin: 6px 0; font-size: 22px; }
.note { background: var(--warn-soft); border-radius: 12px; padding: 10px 12px; margin: 10px 0; font-size: 14px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.rest { margin: 16px 0 6px; }
.rest h3 { margin: 0 2px 6px; font-size: 16px; }
.rest .rest-note { font-size: 13px; color: var(--muted); margin: -2px 2px 8px; }
.menu-card {
  display: flex; gap: 12px; align-items: center; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px; margin: 6px 0;
}
.thumb { width: 52px; height: 52px; border-radius: 10px; object-fit: cover; flex: none; background: var(--accent-soft); display: grid; place-items: center; color: var(--accent); font-weight: 700; }
.menu-card .meta { flex: 1; min-width: 0; }
.menu-card .t { display: block; font-weight: 600; }
.menu-card .s { display: block; font-size: 13px; color: var(--muted); }
.badge { font-size: 11px; border-radius: 6px; padding: 1px 6px; background: var(--warn-soft); margin-left: 6px; }

/* Sheet */
dialog { border: 0; padding: 0; color: var(--ink); }
.sheet {
  width: min(760px, 100%); max-width: 100%; max-height: 92vh; margin: auto auto 0; border-radius: 20px 20px 0 0;
  background: var(--bg); box-shadow: 0 -10px 40px rgb(0 0 0 / 25%);
}
@media (min-width: 800px) { .sheet { margin: auto; border-radius: 20px; } }
.sheet::backdrop, .photo-view::backdrop { background: rgb(10 12 16 / 55%); }
.sheet-head { position: sticky; top: 0; display: flex; align-items: center; gap: 10px; padding: 14px 16px 10px; background: var(--bg); border-bottom: 1px solid var(--line); z-index: 2; }
.sheet-head h3 { margin: 0; flex: 1; font-size: 18px; }
.sheet-body { padding: 12px 16px calc(24px + env(safe-area-inset-bottom)); overflow-y: auto; max-height: calc(92vh - 60px); }
.field { display: block; margin: 12px 0; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input, .field textarea {
  width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-size: 16px;
}
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row > .grow { flex: 1; }
.photos { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.photos figure { position: relative; margin: 0; }
.photos img { width: 84px; height: 84px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); cursor: zoom-in; }
.photos figure button { position: absolute; top: -6px; right: -6px; width: 24px; height: 24px; border-radius: 50%; border: 0; background: var(--ink); color: var(--bg); font-size: 12px; }
.filebtn { position: relative; overflow: hidden; }
.filebtn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.section-title { font-weight: 700; font-size: 14px; color: var(--accent); margin: 16px 2px 4px; }
.edit-dish { display: grid; grid-template-columns: 1fr 96px 32px; gap: 6px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.edit-dish input { padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: 15px; min-width: 0; }
.edit-dish .ru-in { grid-column: 1 / span 2; }
.edit-dish .del { border: 0; background: transparent; color: var(--bad); font-size: 18px; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--accent-soft); border-top-color: var(--accent); animation: spin 0.9s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.status { display: flex; align-items: center; gap: 10px; padding: 12px; border-radius: 12px; background: var(--accent-soft); margin: 10px 0; }
.error { background: var(--bad-soft); color: var(--bad); border-radius: 12px; padding: 10px 12px; margin: 10px 0; }
.muted { color: var(--muted); font-size: 14px; }
.hr { height: 1px; background: var(--line); margin: 18px 0; }
.settings h4 { margin: 18px 0 4px; }
details.help summary { cursor: pointer; color: var(--accent); font-size: 14px; }
details.help { margin: 6px 0; }
details.help ol { padding-left: 20px; font-size: 14px; color: var(--muted); }

.photo-view { background: transparent; max-width: 100vw; max-height: 100vh; width: 100vw; height: 100vh; }
.photo-view img { width: 100%; height: 100%; object-fit: contain; }
.photo-view button { position: fixed; top: calc(12px + env(safe-area-inset-top)); right: 12px; background: rgb(0 0 0 / 55%); color: #fff; }

/* Tabs */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(10px); -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-top: 1px solid var(--line);
}
.tabs a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 600; }
.tabs svg { width: 24px; height: 24px; }
.tabs a.on { color: var(--accent); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabs-h) + 14px + env(safe-area-inset-bottom));
  background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 12px; font-size: 14px; z-index: 50;
  max-width: calc(100vw - 32px); box-shadow: var(--shadow);
}
@media (prefers-reduced-motion: reduce) { .flashcard { transition: none; } }
