:root {
  --bg: #14161c;
  --surface: #1c1f27;
  --surface-2: #262a34;
  --line: #333947;
  --text: #eef0f4;
  --muted: #9aa1ad;
  --accent: #ff8a3d;
  --accent-ink: #1b1207;
  --flag: #ffd166;
  --flag-ink: #1b1505;
  --sel: rgba(255, 138, 61, 0.32);
  --danger: #ff6b6b;
  --radius: 12px;
  color-scheme: dark;
}

[hidden] { display: none !important; }
* { box-sizing: border-box; }

html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  min-height: 100dvh;
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }
button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  min-height: 44px; padding: 0 12px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px;
  color: var(--text); text-decoration: none; cursor: pointer;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
button:active, .btn:active { transform: scale(0.97); }
.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 700; }
.danger { color: var(--danger); }
.grow { flex: 1; }
.muted { color: var(--muted); font-size: 12px; margin: 6px 0 0; }

input[type="url"], input[type="text"], input[type="search"] {
  min-height: 44px; min-width: 0; padding: 0 12px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
}
input[type="range"] { flex: 1; min-width: 0; height: 32px; accent-color: var(--accent); }

.bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; }
.bar h1 { flex: 1; margin: 0; font-size: 22px; letter-spacing: 0.02em; }
.bar h1 span { color: var(--accent); }
.bar h2 {
  flex: 1; min-width: 0; margin: 0;
  font-size: 14px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn { min-width: 44px; padding: 0 10px; background: transparent; border-color: transparent; font-size: 14px; }

/* ===== 一覧 ===== */
#library { max-width: 720px; margin: 0 auto; padding-bottom: 32px; }
.add-form { display: flex; gap: 6px; padding: 0 12px; }
.add-form input { flex: 1; }
.add-form + .add-form { margin-top: 8px; }
.hint { margin: 6px 0 14px; padding: 0 12px; color: var(--muted); font-size: 12px; }

.vlist { display: grid; gap: 8px; margin: 0; padding: 0 12px; list-style: none; }
.vitem { display: flex; align-items: center; gap: 2px; padding: 6px; background: var(--surface); border-radius: var(--radius); }
.vopen { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; }
.vopen img { flex: none; width: 112px; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 8px; background: #000; }
.vmeta { min-width: 0; display: flex; flex-direction: column; }
.vtitle {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  font-size: 14px; line-height: 1.35;
}
.vmeta small { color: var(--muted); font-size: 12px; }
.vitem .icon-btn { color: var(--muted); }
.empty { padding: 32px 16px; color: var(--muted); text-align: center; }
.lib-foot { display: flex; flex-wrap: wrap; gap: 8px; padding: 24px 12px 0; }
.lib-foot .muted { width: 100%; }

/* ===== プレーヤー ===== */
.stage { position: sticky; top: 0; z-index: 5; background: var(--bg); }
.video { position: relative; width: 100%; aspect-ratio: 16 / 9; max-height: 42vh; background: #000; }
#player, .video iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; }
#countdown {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(0, 0, 0, 0.5); color: #fff;
  font-size: 72px; font-weight: 800; pointer-events: none;
}
#playerError {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 16px; background: #000; text-align: center;
}

.controls {
  display: grid; gap: 10px; max-width: 720px; margin: 0 auto;
  padding: 10px 12px calc(24px + env(safe-area-inset-bottom));
}
.card { padding: 10px 12px; background: var(--surface); border-radius: var(--radius); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.card-head h3 { margin: 0; color: var(--muted); font-size: 13px; font-weight: 600; letter-spacing: 0.04em; }
.row { display: flex; align-items: center; gap: 8px; }
.row + .row, .nudges + .row { margin-top: 8px; }

/* タイムライン */
.timerow { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-variant-numeric: tabular-nums; }
#curTime { font-size: 20px; font-weight: 700; }
#selInfo { color: var(--accent); font-size: 13px; font-weight: 600; }
.tlscale { display: flex; justify-content: space-between; margin-right: 22px; color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }

#timeline {
  position: relative; height: 66px; margin: 4px 22px 2px 2px;
  touch-action: none; user-select: none; -webkit-user-select: none; cursor: pointer;
}
.track { position: absolute; left: 0; right: 0; top: 32px; height: 30px; overflow: hidden; background: var(--surface-2); border-radius: 6px; }
#selHL { position: absolute; top: 0; bottom: 0; background: var(--sel); box-shadow: inset 0 -3px 0 var(--accent); }
#playhead { position: absolute; top: 26px; bottom: 0; z-index: 2; width: 2px; margin-left: -1px; background: #fff; pointer-events: none; }
#flagLayer { position: absolute; inset: 0; pointer-events: none; }
.flag.out, #playhead.out { display: none; }
.flag { position: absolute; top: 0; bottom: 0; width: 0; }
.flag::after { content: ""; position: absolute; left: -1px; top: 6px; bottom: 0; width: 2px; background: var(--flag); }
.flag .h {
  position: absolute; left: -1px; top: 0;
  display: grid; place-items: center; min-width: 26px; height: 26px; padding: 0 5px;
  background: var(--flag); color: var(--flag-ink); border-radius: 0 7px 7px 0;
  font-size: 13px; font-weight: 800; cursor: grab; pointer-events: auto;
}
.flag .h::before { content: ""; position: absolute; inset: -8px -6px; }
.flag.active { z-index: 1; }
.flag.active .h, .flag.active::after { background: #fff; }

/* 再生ボタン群 */
.transport { display: grid; grid-template-columns: 1fr 1fr 1.4fr 1fr 1.7fr; gap: 6px; }
.transport button { min-height: 54px; padding: 0 4px; font-size: 15px; }
#playBtn { font-size: 22px; }
.flag-btn { background: var(--flag); color: var(--flag-ink); border-color: transparent; font-weight: 800; }

.seg { display: inline-flex; gap: 3px; padding: 3px; background: var(--surface-2); border-radius: 10px; }
.seg button { min-height: 38px; padding: 0 14px; background: transparent; border: 0; }
.seg button.on { background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.wait { flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.wait output { min-width: 3.2em; color: var(--text); text-align: right; font-variant-numeric: tabular-nums; }
.wait.off { opacity: 0.4; }

/* 区画・フラグのチップ */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { flex-direction: column; align-items: flex-start; padding: 4px 10px; line-height: 1.2; text-align: left; }
.chip small { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 700; }
.chip.on small { color: rgba(27, 18, 7, 0.75); }
.chip.flagchip span { font-weight: 800; }
.chip.flagchip.on { background: #fff; }
.toggle { min-height: 34px; font-size: 13px; }
.toggle.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 700; }
#sectionChips .muted, #flagChips .muted { width: 100%; margin: 0; }

#flagEditor { margin-top: 10px; }
.nudges { display: grid; grid-template-columns: 1fr 1fr 1.3fr 1fr 1fr; gap: 6px; align-items: center; }
.nudges button { padding: 0 4px; }
#flagTime { font-weight: 700; font-variant-numeric: tabular-nums; text-align: center; font-size: 14px; }
#flagName { flex: 1; }

.presets button { flex: 1; }
#rateOut { font-size: 16px; font-variant-numeric: tabular-nums; }
#rateNote { margin: 0; }

/* 説明書モーダル */
.manual-btn { flex: none; color: var(--muted); font-size: 18px; font-weight: 700; }
body:has(#manual[open]) { overflow: hidden; }
#manual {
  width: min(640px, calc(100% - 24px)); max-height: 86dvh; padding: 0;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 16px;
  overflow: hidden; display: none; flex-direction: column;
}
#manual[open] { display: flex; }
#manual::backdrop { background: rgba(0, 0, 0, 0.6); }
.manual-head {
  display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 16px;
  border-bottom: 1px solid var(--line);
}
.manual-head h2 { flex: 1; margin: 0; font-size: 16px; }
.manual-body { overflow-y: auto; overscroll-behavior: contain; padding: 4px 16px 20px; font-size: 14px; line-height: 1.7; }
.manual-body h3 { margin: 18px 0 6px; color: var(--accent); font-size: 15px; }
.manual-body p, .manual-body ul { margin: 0 0 6px; }
.manual-body ul { padding-left: 1.2em; }
.manual-body li + li { margin-top: 4px; }
.manual-body b { color: var(--flag); font-weight: 700; }
.manual-body .tip { padding: 8px 10px; background: var(--surface-2); border-radius: 8px; font-size: 13px; }

#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 50;
  display: flex; align-items: center; gap: 12px;
  max-width: calc(100% - 32px); padding: 10px 14px;
  background: #f4f5f7; color: #111; border-radius: 10px; transform: translateX(-50%);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}
#toast button { min-height: 32px; padding: 0 4px; background: transparent; border: 0; color: #c2410c; font-weight: 700; }

/* 横向き: 動画を左、操作を右に */
@media (orientation: landscape) and (min-width: 700px) {
  #playerView { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(340px, 1fr); height: 100dvh; }
  .stage { position: relative; }
  .video { max-height: calc(100dvh - 60px); }
  .controls { height: 100dvh; overflow-y: auto; max-width: none; margin: 0; }
}
