:root {
  --bg: #0d0d12;
  --bg2: #17171f;
  --bg3: #22222d;
  --line: #34343f;
  --text: #f6f5f2;
  --muted: #b3b1ab;
  --accent: #ff8a3d;
  --accent-ink: #1a0d03;
  --good: #59d38c;
  --warn: #ffcc4d;
  --organ: #c9a2ff;
  --raw: #6fd6e0;
  --danger: #ff6b6b;
  --radius: 16px;
  --tabs-h: 64px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", sans-serif;
  --font-ja: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Noto Sans CJK JP", "Noto Sans JP", "Meiryo", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 18px/1.45 var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  display: flex;
  flex-direction: column;
  height: 100dvh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { touch-action: manipulation; cursor: pointer; }
[hidden] { display: none !important; }

[lang="ja"], .ja {
  font-family: var(--font-ja);
  line-break: strict;
  word-break: keep-all;
  overflow-wrap: anywhere;
}
@supports (word-break: auto-phrase) {
  [lang="ja"], .ja { word-break: auto-phrase; }
}

/* Header */
.top {
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.brand { font-size: 20px; margin: 0; flex: 1; font-weight: 700; letter-spacing: .2px; }
.brand-mark {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font-size: 18px; margin-right: 4px;
}
.net { font-size: 13px; color: var(--muted); }
.net.off { color: var(--warn); }

.icon-btn {
  min-width: 48px; min-height: 48px; border-radius: 12px; border: 0;
  background: var(--bg3); font-size: 22px; display: inline-grid; place-items: center;
}
.icon-btn.big { min-width: 56px; min-height: 56px; font-size: 26px; }
.link-btn { background: none; border: 0; color: var(--accent); padding: 10px 6px; font-size: 16px; text-decoration: underline; text-underline-offset: 3px; }

main { flex: 1; overflow: hidden; position: relative; }
.view { position: absolute; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 12px 16px 24px; }

/* Tabs */
.tabs {
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--bg2); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  background: none; border: 0; min-height: var(--tabs-h); color: var(--muted);
  font-size: 13px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.tabs button span { font-size: 24px; line-height: 1; }
.tabs button.active { color: var(--accent); }

/* Search and chips */
.search-row input, .sheet input, .sheet select, #passcode-input {
  width: 100%; min-height: 50px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg2); padding: 10px 14px; font-size: 17px;
}
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 12px 0; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none; min-height: 44px; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--bg2); font-size: 16px; white-space: nowrap;
}
.chips button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.chips.small button { min-height: 40px; font-size: 15px; }

/* Phrase cards */
.card-list { display: flex; flex-direction: column; gap: 10px; }
.cat-title { margin: 14px 2px 2px; font-size: 15px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .6px; }
.card {
  position: relative; text-align: left; width: 100%;
  background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 56px 14px 16px;
}
.card:active { background: var(--bg3); }
.card .ja { font-size: 25px; line-height: 1.35; font-weight: 600; display: block; }
.card .romaji { color: var(--muted); font-size: 15px; display: block; margin-top: 4px; }
.card .en { font-size: 18px; display: block; margin-top: 4px; }
.card .tip { font-size: 14px; color: var(--muted); display: block; margin-top: 6px; }
.card .fav {
  position: absolute; top: 6px; right: 6px; width: 48px; height: 48px; border: 0; border-radius: 12px;
  background: none; font-size: 24px; color: var(--muted);
}
.card .fav.on { color: var(--warn); }
.badge {
  display: inline-block; margin-top: 8px; font-size: 12px; padding: 3px 8px; border-radius: 999px;
  background: #3a2f12; color: var(--warn);
}
.empty { color: var(--muted); text-align: center; padding: 30px 10px; }

/* Show mode */
.show {
  position: fixed; inset: 0; z-index: 50; background: #000; color: #fff;
  display: flex; flex-direction: column;
  padding: calc(env(safe-area-inset-top) + 8px) 12px calc(env(safe-area-inset-bottom) + 12px);
}
.show.light { background: #fff; color: #000; }
.show.light .icon-btn, .show.light .speak-btn { background: #eee; color: #000; }
.show.light .show-romaji, .show.light .show-en { color: #444; }
.show-bar { display: flex; gap: 8px; justify-content: space-between; }
.show-bar .star { margin-left: auto; }
.show-bar .star.on { color: var(--warn); }
.show-stage { flex: 1; display: flex; align-items: center; justify-content: center; overflow: hidden; padding: 8px 4px; }
.show-text { font-weight: 700; line-height: 1.3; text-align: center; max-width: 100%; white-space: pre-line; }
.show.rotated .show-stage, .show.rotated .show-sub { transform: rotate(180deg); }
.show-sub { text-align: center; padding: 4px 8px 10px; }
.show-romaji { color: #c9c7c1; font-size: 20px; }
.show-en { color: #c9c7c1; font-size: 18px; margin-top: 4px; }
.show-controls { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.speak-btn {
  flex: 1; min-height: 64px; border-radius: 18px; border: 0; background: var(--accent); color: var(--accent-ink);
  font-size: 30px;
}

/* Talk */
.talk { padding: 0; display: flex; flex-direction: column; overflow: hidden; }
.half { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 8px 12px; }
.half.them { transform: rotate(180deg); background: #14141c; }
.half-head { font-size: 15px; color: var(--muted); text-align: center; padding: 2px 0 6px; }
.divider {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 0 12px; min-height: 40px; background: var(--bg2); border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
  font-size: 13px; color: var(--muted);
}
.divider .link-btn { font-size: 14px; padding: 6px; }
.log { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; padding: 4px 0 8px; }
.bubble { max-width: 92%; border-radius: 16px; padding: 10px 14px; background: var(--bg3); }
.bubble.mine { align-self: flex-end; background: #3a2414; }
.bubble.theirs { align-self: flex-start; }
.bubble .main { font-size: 22px; line-height: 1.35; }
.bubble.big .main { font-size: 26px; font-weight: 600; }
.bubble .sub { font-size: 14px; color: var(--muted); margin-top: 4px; }
.bubble .note { font-size: 14px; color: var(--warn); margin-top: 6px; }
.bubble .err { font-size: 14px; color: var(--danger); margin-top: 6px; }
.bubble .say { float: right; margin: -4px -6px 0 8px; min-width: 44px; min-height: 44px; border: 0; background: none; font-size: 20px; }
.bubble.pending .main::after { content: " …"; color: var(--muted); }
.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1; min-height: 52px; max-height: 120px; resize: none; border-radius: 14px; border: 1px solid var(--line);
  background: var(--bg2); padding: 12px; font-size: 18px;
}
.composer .mic, .composer .send {
  min-width: 52px; min-height: 52px; border-radius: 14px; border: 0; background: var(--bg3); font-size: 20px;
}
.composer .send { background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 16px; padding: 0 12px; }
.composer .mic.listening { background: var(--danger); }
.talk-hint { font-size: 14px; color: var(--muted); text-align: center; padding: 10px; }

/* Menu */
.menu-actions { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
.big-btn {
  display: flex; align-items: center; justify-content: center; text-align: center; gap: 6px;
  min-height: 56px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg2);
  font-size: 17px; font-weight: 600; padding: 10px 14px; cursor: pointer;
}
.big-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.hint { color: var(--muted); font-size: 15px; }
.hint.small { font-size: 13px; }
.menu-photo { width: 100%; max-height: 200px; object-fit: contain; border-radius: 12px; background: #000; margin: 6px 0; }
.menu-summary { background: var(--bg2); border-radius: 12px; padding: 12px 14px; margin: 8px 0; font-size: 16px; }
.menu-section { margin: 16px 2px 4px; color: var(--muted); font-size: 14px; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.item {
  background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin: 8px 0;
}
.item-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.item .ja { font-size: 24px; font-weight: 600; }
.item .price { text-align: right; font-weight: 700; font-size: 18px; white-space: nowrap; }
.item .price small { display: block; font-weight: 500; color: var(--good); font-size: 16px; }
.item .romaji { color: var(--muted); font-size: 15px; }
.item .en { font-size: 18px; font-weight: 600; margin-top: 4px; }
.item .what { font-size: 16px; margin-top: 2px; }
.item .try { font-size: 15px; color: var(--good); margin-top: 4px; }
.flags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.flag { font-size: 13px; padding: 3px 9px; border-radius: 999px; background: #3a2f12; color: var(--warn); }
.flag.organ { background: #2c2140; color: var(--organ); }
.flag.raw { background: #12333a; color: var(--raw); }
.item-actions { display: flex; gap: 8px; margin-top: 8px; }
.item-actions button { min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg3); padding: 6px 12px; font-size: 15px; }
.status { color: var(--muted); padding: 8px 2px; }
.error { color: var(--danger); padding: 8px 2px; }

/* About me */
.about-card { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.about-card .ja-lines { font-size: 23px; line-height: 1.6; font-weight: 600; }
.about-card .ja-lines p, .about-card .en-lines p { margin: 0 0 6px; }
.about-card .en-lines { color: var(--muted); font-size: 16px; margin-top: 14px; border-top: 1px solid var(--line); padding-top: 12px; }
.about-card .nudge { color: var(--warn); font-size: 15px; margin-bottom: 10px; }
.row-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 14px 0 6px; }

/* Tools */
.panel { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.conv-label { color: var(--muted); font-size: 15px; }
.conv-row { display: flex; align-items: center; gap: 6px; }
.conv-sym { font-size: 34px; font-weight: 700; color: var(--muted); }
.conv-row input { flex: 1; min-width: 0; font-size: 40px; font-weight: 700; background: none; border: 0; border-bottom: 2px solid var(--line); padding: 4px 0; }
.conv-out { font-size: 34px; font-weight: 700; color: var(--good); margin: 8px 0 2px; }
.conv-rev { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 16px; }
.conv-rev input { width: 110px; min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); padding: 6px 10px; }
.rate-line { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 6px; margin-top: 10px; font-size: 14px; color: var(--muted); }
.section-title { font-size: 20px; margin: 22px 2px 8px; }
details.cheat { background: var(--bg2); border: 1px solid var(--line); border-radius: 14px; margin: 8px 0; }
details.cheat summary { padding: 14px 16px; font-size: 18px; font-weight: 600; min-height: 52px; list-style: none; }
details.cheat summary::-webkit-details-marker { display: none; }
details.cheat ul { margin: 0; padding: 0 16px 14px 34px; }
details.cheat li { margin: 6px 0; font-size: 16px; }
.usd { color: var(--good); white-space: nowrap; }

/* Dialogs */
dialog { border: 0; padding: 0; background: transparent; color: var(--text); max-width: 100vw; width: 100%; }
dialog::backdrop { background: rgba(0, 0, 0, .7); }
#settings { height: 100%; max-height: 100%; margin: 0; }
.sheet {
  background: var(--bg); border-radius: 0; padding: calc(env(safe-area-inset-top) + 12px) 18px calc(env(safe-area-inset-bottom) + 24px);
  min-height: 100%; display: flex; flex-direction: column; gap: 8px;
}
#passcode-dialog { margin: auto; max-width: 420px; }
#passcode-dialog .sheet { border-radius: 18px; min-height: 0; padding: 20px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet h2 { margin: 0; font-size: 24px; }
.sheet h3 { margin: 18px 0 0; font-size: 17px; color: var(--accent); }
.sheet label { display: flex; flex-direction: column; gap: 4px; font-size: 15px; color: var(--muted); }
.sheet label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text); min-height: 48px; }
.sheet label.check input { width: 24px; height: 24px; min-height: 0; }
.sheet-foot { position: sticky; bottom: 0; padding-top: 10px; background: var(--bg); }
.sheet-foot .big-btn { width: 100%; }

.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 12px); z-index: 60;
  background: #2a2a35; border: 1px solid var(--line); border-radius: 14px; padding: 12px 16px; font-size: 16px; text-align: center;
}

/* "Where are you?" quick mode */
.places-label { font-size: 14px; color: var(--muted); margin: 12px 0 6px; }
.places { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.places button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 68px; padding: 6px 4px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg2); color: var(--text); font: 600 13px/1.2 var(--font); text-align: center;
}
.places button .pi { font-size: 24px; line-height: 1; }
.places button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.place-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* Camera: what to do, phrase to say, follow-up questions */
.menu-do { background: var(--bg2); border-left: 4px solid var(--good); border-radius: 12px; padding: 12px 14px; margin: 8px 0; font-size: 16px; }
.menu-say, .qa { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin: 8px 0; }
.menu-say .ja, .qa .ja { font-size: 22px; }
.menu-say .romaji, .qa .romaji { color: var(--muted); font-size: 15px; }
.qa .q { color: var(--accent); font-weight: 600; margin-bottom: 4px; }
.qa .a { margin-bottom: 6px; }
.ask-photo { display: flex; gap: 8px; margin: 12px 0 4px; }
.ask-photo input { flex: 1; min-width: 0; min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg3); color: var(--text); padding: 0 12px; font-size: 17px; }
.ask-photo button { min-height: 48px; padding: 0 18px; border-radius: 12px; border: 0; background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 16px; }

/* Talk and Camera locked to "coming soon" without an access code */
.soon { display: none; text-align: center; padding: 48px 20px; }
.ai-locked .soon { display: block; }
.ai-locked #view-talk > :not(.soon), .ai-locked #view-menu > :not(.soon) { display: none !important; }
.ai-locked #view-talk { display: block; }
.soon-icon { font-size: 48px; }
.soon h2 { margin: 8px 0; font-size: 26px; }
.soon p { color: var(--text); max-width: 34ch; margin: 8px auto; }
.soon .hint { color: var(--muted); }
