:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --border: #d2d2d7;
  --accent: #0a66d8;
  --accent-text: #ffffff;
  --ok: #1a7f37;
  --ok-bg: #dafbe1;
  --miss: #c62828;
  --miss-bg: #ffe3e3;
  --partial: #9a6700;
  --partial-bg: #fff3c4;
  --mark: #d97706;
  --rec: #d93025;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --text: #f5f5f7;
    --muted: #a1a1a6;
    --border: #3a3a3c;
    --accent: #4c9bff;
    --accent-text: #000000;
    --ok: #57d07a;
    --ok-bg: #12351d;
    --miss: #ff7b72;
    --miss-bg: #4a1414;
    --partial: #e3b341;
    --partial-bg: #3d2e00;
    --mark: #f5a524;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; overflow-x: clip; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", sans-serif;
  line-height: 1.5;
  /* 下の余白は、固定の操作パネルで最後のカードが隠れない分をとる */
  padding: 0 max(16px, env(safe-area-inset-right)) calc(140px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
/* 操作パネルを隠しているときは、隅の「…」で最後のカードが隠れない分だけあける */
html[data-player="off"] body { padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
/* ヘッダーを低くするため、サブタイトルはタイトルの右に並べる（狭い画面では次の行に回る） */
.top h1 {
  font-size: 17px; margin: 8px 0 6px;
  display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 6px;
}
.subtitle { font-size: 11px; font-weight: 400; color: var(--muted); white-space: nowrap; }
main { max-width: 720px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
/* タイトルの行 */
.top { max-width: 720px; margin: 0 auto; display: flex; align-items: flex-start; gap: 8px; }
.top h1 { flex: 1; min-width: 0; }
/* 今の文章の名前。タイトルの下に、幅いっぱいで置く */
.top { flex-wrap: wrap; }
/* スクロールしてもタイトル・📄・↻ が見えるよう、上に貼りつける */
.top {
  position: sticky; top: 0; z-index: 5;
  padding: env(safe-area-inset-top) 0 6px;
}
/* 背景と下線は画面の左右いっぱいまで広げる（青い枠などが横からはみ出して見えないように） */
.top::before {
  content: ""; position: absolute; z-index: -1;
  top: 0; bottom: 0; left: 50%; width: 100vw; transform: translateX(-50%);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.doc-row { flex: 1 0 100%; min-width: 0; display: flex; gap: 8px; }
#btn-reload { flex: none; font-size: 18px; }
.doc-switch {
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: 6px;
  text-align: left; font-weight: 600;
}
#doc-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.panel, .card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
summary { font-weight: 600; cursor: pointer; padding: 2px 0; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0; }
.notes-lead { font-size: 15px; margin: 8px 0 0; }
.notes-lead strong { color: var(--accent); }
.notes { font-size: 14px; margin: 8px 0 0; padding-left: 1.2em; }
.notes li { margin-bottom: 6px; }
.notes ul { padding-left: 1.2em; margin: 4px 0; }

textarea, input[type="text"], select {
  width: 100%;
  font: inherit;
  font-size: 16px; /* iOS で入力時に拡大されないよう 16px 以上にする */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
}
textarea { resize: vertical; }
.source-count { margin: 4px 2px 0; font-size: 13px; color: var(--muted); text-align: right; }
.source-count.over { color: var(--miss); font-weight: 600; }

.row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
button, .button {
  font: inherit;
  font-size: 15px;
  min-height: 44px;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
button:active, .button:active { opacity: 0.6; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--miss); }
#btn-reset { margin-top: 12px; }
button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.recording { background: var(--rec); color: #fff; border-color: var(--rec); }

#photo-view { margin-top: 10px; }
#photo-view img { width: 100%; border-radius: 10px; display: block; margin-bottom: 8px; }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.section-head h2 { font-size: 17px; margin: 4px 0; }
.section-head { flex-wrap: wrap; }

#sentences { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.card .en { font-size: 18px; margin: 0; }
/* カードの上の行。番号「2/10」としおりボタン。左右設定と反対側に置く */
.card-head { display: flex; justify-content: flex-start; align-items: center; min-height: 36px; margin-bottom: -4px; }
html[data-hand="left"] .card-head { justify-content: flex-end; }
.card-head .num { padding: 0 4px; color: var(--muted); font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
/* 番号としおりボタン。左手設定では並びを逆にして、しおりが内側に来るようにする */
.card-head .head-side { display: flex; align-items: center; gap: 2px; }
html[data-hand="left"] .card-head .head-side { flex-direction: row-reverse; }
.card-head .btn-mark { padding: 4px 8px; font-size: 16px; line-height: 1; background: transparent; border-color: transparent; opacity: 0.35; }
.card-head .btn-mark[aria-pressed="true"] { opacity: 1; background: var(--mark); border-color: var(--mark); }
.card.marked { border-color: var(--mark); border-left: 6px solid var(--mark); }
/* 今の文（操作パネルで読む・動く基準） */
.card.current { outline: 2px solid var(--accent); outline-offset: 2px; }
.card.speaking .en { color: var(--accent); }
.ja-view { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.ja-text { margin: 0; font-size: 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.ja-text.empty { color: var(--muted); }
.card.speaking-ja .ja-text { color: var(--accent); }
.card .row .row-spacer { flex: 1 1 0; padding: 0 6px; border: 1px solid transparent; } /* ボタンと同じ幅になるよう余白と枠を揃える */
.card .row { flex-wrap: nowrap; }
.card .row button { flex: 1 1 0; min-width: 0; padding: 8px 6px; white-space: nowrap; }
.type-box { display: flex; gap: 8px; margin-top: 10px; }
.type-box input { flex: 1; }

.result { margin-top: 10px; padding: 10px; border-radius: 10px; background: var(--bg); }
.score { font-weight: 700; font-size: 20px; }
.score.good { color: var(--ok); }
.score.fair { color: var(--partial); }
.score.poor { color: var(--miss); }
.diff { margin: 6px 0 0; font-size: 17px; line-height: 1.9; }
.heard { margin: 6px 0 0; font-size: 14px; color: var(--muted); }
.w { border-radius: 5px; padding: 1px 3px; }
.w.ok { color: var(--ok); background: var(--ok-bg); }
.w.miss { color: var(--miss); background: var(--miss-bg); text-decoration: underline wavy; }
.w.partial { color: var(--partial); background: var(--partial-bg); }
.w.extra { color: var(--muted); text-decoration: line-through; }

#translate-panel .row { align-items: center; }
.info-button {
  width: 32px; min-height: 32px; height: 32px; padding: 0;
  border-radius: 50%;
  border: 1.5px solid var(--accent);
  color: var(--accent);
  font-family: Georgia, "Times New Roman", serif;
  font-style: italic; font-weight: 700; font-size: 17px;
}
.info-button[aria-expanded="true"] { background: var(--accent); color: var(--accent-text); }
.steps { padding-left: 1.4em; }
.paste-ja { margin-top: 14px; padding-top: 4px; border-top: 1px solid var(--border); }
.paste-ja textarea { margin-top: 10px; }
.source-count.done { color: var(--ok); }
a.button { text-decoration: none; }
a.button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }

/* 翻訳ページ */
.translate-nav { display: flex; align-items: center; justify-content: space-between; margin: 12px auto; }
.status { color: var(--muted); font-size: 14px; }
#translate-area { font-size: 18px; }
#translate-area p { margin: 0 0 12px; }
#translate-area p:last-child { margin-bottom: 0; }

.field { display: block; margin-top: 10px; font-size: 15px; }
.field input[type="range"] { width: 100%; margin-top: 6px; }
.field select { margin-top: 6px; }
/* 狭い画面でも1行に収まるよう、「英語」などの文字は回数の上に置く */
.play-order { display: flex; align-items: flex-end; gap: 8px; margin-top: 6px; }
.play-order label { display: flex; flex-direction: column; font-size: 13px; color: var(--muted); }
.play-order select { width: auto; margin-top: 2px; }
.play-order span { padding-bottom: 12px; }

/* 画面下の操作パネル。右手設定の並びは左から しおり／設定・全部読む・前へ・次へ・英語／日本語。左手設定では左右を反転する */
.player {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 10;
  max-width: 720px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1.3fr;
  grid-template-rows: 52px 52px;
  grid-template-areas: "mark all prev next en" "set all prev next ja";
  gap: 6px;
  padding: 8px max(8px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 14px rgb(0 0 0 / 0.12);
}
html[data-hand="left"] .player {
  grid-template-columns: 1.3fr 1fr 1fr 1fr 1fr;
  grid-template-areas: "en next prev all mark" "ja next prev all set";
}
.player button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-width: 0; min-height: 0; padding: 4px 2px;
  font-size: 20px; line-height: 1.1;
}
.player button span { font-size: 11px; white-space: nowrap; }
.player button:disabled { opacity: 0.35; }
.player .p-mark { grid-area: mark; }
.player .p-set { grid-area: set; }
.player .p-all { grid-area: all; }
.player .p-prev { grid-area: prev; }
.player .p-next { grid-area: next; }
.player .p-en { grid-area: en; }
.player .p-ja { grid-area: ja; }

/* 操作パネルを隠しているときの小さな「…」。画面の下の真ん中に置く */
.more-btn {
  position: fixed; z-index: 10;
  bottom: calc(14px + env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  width: 48px; height: 48px; min-height: 0; padding: 0;
  border-radius: 50%;
  font-size: 22px; font-weight: 700; line-height: 1;
  box-shadow: 0 2px 10px rgb(0 0 0 / 0.18);
}
.more-btn:active { transform: translateX(-50%); }

/* 「…」から開くメニュー。パネルを隠しているときは「…」の真上、出しているときは左右設定の側に出す */
.menu {
  position: fixed;
  inset: auto max(14px, env(safe-area-inset-right)) calc(72px + env(safe-area-inset-bottom)) auto;
  margin: 0;
  min-width: 200px;
  padding: 6px;
  display: none;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
}
.menu[open] { display: flex; flex-direction: column; gap: 6px; }
html[data-player="off"] .menu { right: auto; left: 50%; transform: translateX(-50%); }
html[data-player="on"] .menu { bottom: calc(132px + env(safe-area-inset-bottom)); }
html[data-player="on"][data-hand="left"] .menu { right: auto; left: max(14px, env(safe-area-inset-left)); }
.menu::backdrop { background: rgb(0 0 0 / 0.15); }
.menu button { justify-content: flex-start; border-color: transparent; font-size: 16px; }

/* 設定：メニューから開く、下からせり上がるシート */
.sheet {
  position: fixed;
  inset: auto 0 0 0;
  margin: 0 auto;
  width: 100%;
  max-width: 720px;
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 0 max(16px, env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.sheet[open] { animation: sheet-up 0.2s ease-out; }
@keyframes sheet-up { from { transform: translateY(100%); } }
.sheet::backdrop { background: rgb(0 0 0 / 0.4); }
/* 下までスクロールしても閉じられるよう、見出しは上に貼りつける */
.sheet-head {
  position: sticky; top: 0; z-index: 1;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 12px 0 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.sheet-head h2 { font-size: 17px; margin: 0; }
html:has(.sheet[open]) { overflow: hidden; } /* 開いている間は後ろのページを動かさない */

/* 左右設定 */
.hand-switch { gap: 0; margin-top: 6px; }
.hand-switch button { min-width: 72px; font-weight: 600; }
.hand-switch button:first-child { border-radius: 10px 0 0 10px; }
.hand-switch button:last-child { border-radius: 0 10px 10px 0; border-left: none; }
.hand-switch button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.hand-switch button[aria-pressed="true"]::before { content: "✓ "; white-space: pre; }

/* 片手で届くよう、ボタン類は画面の端から約2/3の幅に収める。
   32px はパネルの外側の余白・枠線・内側の余白の合計 */
:root { --reach: min(100%, calc(100vw * 2 / 3 - 32px)); }
html[data-hand] .row { max-width: var(--reach); }

html[data-hand="right"] .row { margin-left: auto; justify-content: flex-end; }
html[data-hand="right"] #btn-photo-close,
html[data-hand="right"] #btn-reset { display: flex; margin-left: auto; }
html[data-hand="right"] .translate-nav { flex-direction: row-reverse; }

/* 左右設定のボタンだけは寄せずに、幅いっぱいに左半分「左」・右半分「右」と並べる */
html[data-hand] .row.hand-switch { max-width: none; margin-left: 0; }
.hand-switch button { flex: 1 1 0; }

html[data-hand="left"] .sheet-head { flex-direction: row-reverse; }

/* 文章の一覧 */
#btn-new-doc { width: 100%; margin-top: 4px; }
.docs-list { list-style: none; margin: 12px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.doc-item { display: flex; align-items: stretch; gap: 6px; }
.doc-item .doc-open { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left; }
.doc-item.current .doc-open { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.doc-title { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.doc-meta { font-size: 12px; color: var(--muted); }
.doc-item .doc-icon { flex: 0 0 44px; padding: 0; font-size: 18px; }
html[data-hand="left"] .doc-item { flex-direction: row-reverse; }
html[data-hand="left"] .type-box button { order: -1; }
/* 「聞く」は寄せた側の端に置く。訳の「聞く」も同じ並びにして、英文の「聞く」の真下に来るようにする */
html[data-hand="left"] .card .row { flex-direction: row-reverse; }
/* 読み込み直後にパネルが一瞬見えないよう、hidden を付ける前から隠す */
html[data-player="off"] .player { display: none; }
