:root {
  --bg: #0b0d13;
  --panel2: #1f2330;
  --panel-glass: rgba(23, 26, 36, .58);
  --panel-glass-strong: rgba(23, 26, 36, .74);
  --glass-border: rgba(255, 255, 255, .08);
  --glass-highlight: rgba(255, 255, 255, .05);
  --line: #2b3040;
  --text: #eef0f6;
  --muted: #9099b0;
  --accent: #ff8a3d;
  --accent-soft: #ffab61;
  --accent-press: #e6752c;
  --accent-ink: #21120a;
  --accent-bg: rgba(255, 138, 61, .16);
  --accent2: #45c9c4;
  --accent2-bg: rgba(69, 201, 196, .16);
  --danger: #ff5d6c;
  --danger-bg: rgba(255, 93, 108, .16);
  --warn: #ffd166;
  --warn-bg: rgba(255, 209, 102, .16);
  --ok: #5ee38f;
  --ok-bg: rgba(94, 227, 143, .16);
  --text-secondary: #b7bcca;
  --text-strong: #dde1ee;
  /* 読む/書く面（.card, .cut, details.adv）用の不透明サーフェス。--panel-glass相当の質感を維持しつつ不透明にする */
  --surface: #161a24;
  --surface-2: #1e2230;
  --fs-xs: 12px;
  --fs-sm: 14px;
  --fs-base: 16px;
  --fs-lg: 20px;
  --fs-xl: 24px;
  --radius: 16px;
  --tap: 48px;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .25);
  --shadow-md: 0 1px 1px rgba(0, 0, 0, .2), 0 10px 24px rgba(0, 0, 0, .28), 0 28px 56px rgba(0, 0, 0, .2);
  --shadow-glow: 0 10px 28px rgba(255, 138, 61, .32);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: var(--fs-base); line-height: 1.6;
  background: var(--bg);
  height: 100%; overflow: hidden;
}
/* isolation: body自身をスタッキングコンテキスト化しないと、z-index:-1のオーロラが
   このプロパティ無しでは root 側に逃げて body の不透明背景の裏に完全に隠れてしまう */
body { position: relative; isolation: isolate; }

/* --- オーロラ背景（アンビエントな彩り。ガラス面の下に敷く） --- */
body::before, body::after {
  content: ''; position: fixed; z-index: -1; border-radius: 50%; pointer-events: none;
  filter: blur(100px); opacity: .22;
}
body::before {
  width: 640px; height: 640px; top: -220px; left: -160px;
  background: radial-gradient(circle at 30% 30%, var(--accent), transparent 70%);
  animation: drift1 28s ease-in-out infinite alternate;
}
body::after {
  width: 560px; height: 560px; bottom: -200px; right: -160px;
  background: radial-gradient(circle at 70% 70%, var(--accent2), transparent 70%);
  animation: drift2 32s ease-in-out infinite alternate;
}
@keyframes drift1 { from { transform: translate(0, 0) scale(1); } to { transform: translate(70px, 50px) scale(1.1); } }
@keyframes drift2 { from { transform: translate(0, 0) scale(1); } to { transform: translate(-60px, -40px) scale(1.06); } }
@media (prefers-reduced-motion: reduce) { body::before, body::after { animation: none; } }

::selection { background: var(--accent); color: var(--accent-ink); }
* { scrollbar-color: var(--line) transparent; scrollbar-width: thin; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: var(--line); border-radius: 999px; }
*::-webkit-scrollbar-thumb:hover { background: var(--muted); }
.hidden { display: none !important; }
.screen {
  height: 100vh; height: 100dvh;
  display: flex; flex-direction: column; overflow: hidden;
}

/* --- ログイン --- */
#login { display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-mark {
  display: block; width: 64px; height: 64px; margin: 0 auto 6px;
  filter: drop-shadow(0 8px 20px rgba(255, 138, 61, .35));
}
.login-box {
  width: 100%; max-width: 360px;
  background: var(--panel-glass-strong);
  backdrop-filter: blur(28px) saturate(160%);
  -webkit-backdrop-filter: blur(28px) saturate(160%);
  padding: 32px 24px; border-radius: 20px; border: 1px solid var(--glass-border);
  box-shadow: var(--shadow-md), inset 0 1px 0 var(--glass-highlight);
  display: flex; flex-direction: column; gap: 16px;
}
.login-box h1 {
  margin: 0 0 4px; font-size: var(--fs-xl); font-weight: 800; text-align: center; letter-spacing: -.02em;
  color: var(--text);
}

/* --- バー --- */
.bar {
  flex-shrink: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top));
  /* バー自体は全幅背景のまま、中身だけ本文と同じ720px列に揃える。
     720px未満では calc(...) が負になり max() で従来の14pxへフォールバックする */
  padding-inline: max(14px, calc((100% - 720px) / 2));
  background: var(--panel-glass-strong);
  backdrop-filter: blur(18px) saturate(150%);
  -webkit-backdrop-filter: blur(18px) saturate(150%);
  border-bottom: 1px solid var(--glass-border);
}
.bar-title { flex: 1; font-weight: 700; font-size: var(--fs-base); letter-spacing: -.01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dirty-dot {
  flex-shrink: 0; font-size: var(--fs-xs); font-weight: 700; color: var(--warn);
  background: var(--warn-bg); padding: 4px 9px; border-radius: 999px;
}

/* --- 編集画面 サブバー（合計尺 + カットチップナビ。スクロールしても残る） --- */
.editor-subbar {
  flex-shrink: 0; z-index: 9;
  padding: 8px 14px;
  /* バー自体は全幅背景のまま、中身（合計尺/チップ列）だけ本文と同じ720px列に揃える */
  padding-inline: max(14px, calc((100% - 720px) / 2));
  background: var(--panel-glass-strong);
  backdrop-filter: blur(18px) saturate(150%);
  -webkit-backdrop-filter: blur(18px) saturate(150%);
  border-bottom: 1px solid var(--glass-border);
  display: flex; flex-direction: column; gap: 6px;
}
.duration-total {
  font-size: var(--fs-xs); font-weight: 700; color: var(--text-secondary);
  font-variant-numeric: tabular-nums; min-height: 1em;
}
.duration-total.warn { color: var(--warn); }
.cut-chips {
  display: flex; gap: 6px; overflow-x: auto; -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.cut-chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0; padding: 0 14px;
  font-family: ui-monospace, "SF Mono", monospace; font-size: var(--fs-xs); font-weight: 800;
  background: var(--panel2); color: var(--muted); border: 1px solid var(--line);
}
.chip.chip-ai { color: var(--accent); background: var(--accent-bg); border-color: transparent; }
.chip.chip-kb { color: var(--accent2); background: var(--accent2-bg); border-color: transparent; }
/* 現在ビューポート内にあるカットのチップ（IntersectionObserverで追従） */
.chip.chip-active { box-shadow: 0 0 0 2px var(--text-strong) inset; filter: brightness(1.1); }

/* --- ボタン・入力 --- */
button {
  min-height: var(--tap); padding: 0 18px; font-size: var(--fs-base); font-weight: 600;
  border: none; border-radius: 12px; cursor: pointer; color: var(--text);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--panel2); transition: background .15s var(--ease), transform .1s var(--ease), filter .15s var(--ease), box-shadow .15s var(--ease);
}
.icon { display: block; flex-shrink: 0; }
/* アイコン単独ボタン（並べ替え/削除/戻る等）。見た目は小さくても44x44を確保する */
.icon-btn { padding: 0; min-height: 44px; min-width: 44px; }
button:active { transform: scale(.96); }
button:hover { filter: brightness(1.08); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary {
  background:
    radial-gradient(120% 180% at 50% -20%, rgba(255, 255, 255, .35), transparent 60%),
    linear-gradient(135deg, var(--accent), var(--accent-soft));
  color: var(--accent-ink); font-weight: 800;
  box-shadow: var(--shadow-glow), 0 2px 4px rgba(0, 0, 0, .2);
}
button.primary:hover { filter: brightness(1.05); }
button.primary:active { box-shadow: 0 4px 12px rgba(255, 138, 61, .3); }
button.ghost { background: transparent; padding: 0 12px; color: var(--muted); }
button.ghost:hover { color: var(--text); filter: none; }
button.small { min-height: 44px; min-width: 44px; font-size: var(--fs-sm); padding: 0 14px; }

input, textarea, select {
  width: 100%; background: var(--panel2); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; font-size: var(--fs-base); font-family: inherit; line-height: 1.5;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg);
}
input[readonly], textarea[readonly] { color: var(--muted); }
/* resize:none — auto-grow(JS)がscrollHeightに追従するため、手動リサイズと競合させない */
textarea { resize: none; min-height: 52px; max-height: 24em; overflow-y: auto; }

/* --- 一覧: モード切替タブ --- */
.mode-tabs { display: flex; gap: 4px; padding: 10px 14px 0; }
.mode-tabs .tab {
  flex: 1; padding: 10px 0; background: transparent; color: var(--muted);
  border: 1px solid var(--glass-border); border-radius: 10px; font-weight: 600; font-size: var(--fs-sm);
}
.mode-tabs .tab.active { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }

/* --- 一覧 --- */
.list-body {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 14px; padding-bottom: calc(14px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 12px;
}
/* flex-shrink:0: 縦flexスクロールコンテナ直下の子はshrinkさせない。
   overflowがvisible以外の子(details.adv等)はmin-height:autoの自動保護が効かず、
   flex-shrink既定値(1)のままだと高さ0まで潰れてしまうため */
.list-body > * { max-width: 720px; margin-inline: auto; width: 100%; flex-shrink: 0; }
.card {
  background: var(--surface);
  border: 1px solid var(--glass-border); border-radius: var(--radius);
  padding: 16px; cursor: pointer;
  display: flex; align-items: center; gap: 10px;
  box-shadow: var(--shadow-sm), inset 0 1px 0 var(--glass-highlight);
  transition: background .15s var(--ease), border-color .15s var(--ease), transform .15s var(--ease), box-shadow .15s var(--ease);
}
.card:hover { border-color: var(--accent); box-shadow: var(--shadow-md), inset 0 1px 0 var(--glass-highlight); transform: translateY(-2px); }
.card:active { transform: translateY(0); }
.card.plain { cursor: default; box-shadow: none; }
.card.plain:hover { border-color: var(--glass-border); box-shadow: none; transform: none; }
.card.loading { color: var(--muted); animation: pulse 1.1s ease-in-out infinite; }
.msg-line { display: flex; align-items: center; gap: 6px; }
.card-main { flex: 1; min-width: 0; }
.card h2 { margin: 0 0 6px; font-size: var(--fs-base); font-weight: 700; letter-spacing: -.01em; }
.card .meta { color: var(--muted); font-size: var(--fs-xs); font-variant-numeric: tabular-nums; }
.card-arrow { flex-shrink: 0; font-size: 22px; color: var(--accent); }
@keyframes pulse { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }

/* --- 編集 --- */
.editor-body {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 14px; padding-bottom: calc(14px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
/* flex-shrink:0: 縦flexスクロールコンテナ直下の子はshrinkさせない。
   overflowがvisible以外の子(details.adv等)はmin-height:autoの自動保護が効かず、
   flex-shrink既定値(1)のままだと高さ0まで潰れてしまうため */
.editor-body > * { max-width: 720px; margin-inline: auto; width: 100%; flex-shrink: 0; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: var(--fs-xs); color: var(--text-secondary); font-weight: 600; }
.field .hint { font-size: var(--fs-xs); color: var(--muted); min-height: 1em; }
/* .field 配下に限定しない: warnBlock() は .adv-body 直下に置かれるため */
.hint.warn {
  display: flex; align-items: flex-start; gap: 6px; color: var(--warn); background: var(--warn-bg); font-weight: 700;
  font-size: var(--fs-sm); line-height: 1.5; padding: 8px 12px; border-radius: 8px;
}
.hint.warn .icon { margin-top: 1px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.section-title {
  position: relative; font-size: var(--fs-xs); color: var(--muted); font-weight: 800;
  letter-spacing: .08em; padding: 2px 0 2px 13px;
  /* margin-inline:auto を親の `.editor-body > *` が効かせるため、margin は block 方向のみ指定する
     (margin ショートハンドは同詳細度で margin-inline:auto を上書きしてしまう) */
  margin-block-start: 12px; margin-block-end: 0;
}
.section-title::before {
  content: ''; position: absolute; left: 0; top: 1px; bottom: 1px; width: 3px;
  background: linear-gradient(180deg, var(--accent), var(--accent2));
  border-radius: 2px;
}
.section-title:first-child { margin-block-start: 0; }

.cut {
  background: var(--surface);
  border: 1px solid var(--glass-border); border-left: 4px solid var(--accent);
  border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 12px;
  box-shadow: var(--shadow-sm), inset 0 1px 0 var(--glass-highlight);
  transition: box-shadow .2s var(--ease);
}
.cut:focus-within { box-shadow: var(--shadow-md), inset 0 1px 0 var(--glass-highlight); }
.cut[data-mode="kenburns"] { border-left-color: var(--accent2); }
.cut-head { display: flex; align-items: center; gap: 8px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.cut-head .badge {
  background: var(--panel2); border-radius: 8px; padding: 4px 10px; font-size: var(--fs-xs); font-weight: 700;
}
.cut-head .badge.cut-no {
  font-family: ui-monospace, "SF Mono", monospace; font-weight: 800;
  letter-spacing: .02em; min-width: 52px; text-align: center;
}
.cut-head .mode-ai { color: var(--accent); background: var(--accent-bg); }
.cut-head .mode-kb { color: var(--accent2); background: var(--accent2-bg); }
.cut-head .grow { flex: 1; }
.cut-head .cut-del {
  flex-shrink: 0; background: transparent; color: var(--muted);
}
.cut-head .cut-del:hover, .cut-head .cut-del:focus-visible { color: var(--danger); filter: none; }

/* カード折りたたみ: summary=サマリ行（常時表示、タップで開閉）、cut-fields=展開時の全フィールド */
.cut-body { background: transparent; }
.cut-body > summary {
  list-style: none; cursor: pointer; display: flex; align-items: center; gap: 10px; min-height: 44px;
}
.cut-body > summary::-webkit-details-marker { display: none; }
.cut-summary-chevron { flex-shrink: 0; color: var(--muted); transition: transform .18s var(--ease); }
.cut-body[open] > summary .cut-summary-chevron { transform: rotate(180deg); }
.cut-summary-excerpt {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: var(--fs-sm); color: var(--text-secondary);
}
.cut-summary-dur {
  flex-shrink: 0; font-size: var(--fs-xs); font-weight: 700; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.cut-fields { display: flex; flex-direction: column; gap: 12px; padding-top: 12px; }

.note {
  background: rgba(0, 0, 0, .18); border-left: 3px solid var(--line); border-radius: 8px;
  padding: 8px 12px; font-size: var(--fs-xs); color: var(--muted); white-space: pre-wrap;
}
.note b { color: var(--text-strong); font-weight: 600; }

/* 配列（must_show など） */
.arr { display: flex; flex-direction: column; gap: 8px; }
.arr-item { display: flex; gap: 8px; align-items: center; }
.arr-item input { flex: 1; }
.arr-tone-icon { flex-shrink: 0; width: 20px; text-align: center; font-size: var(--fs-sm); font-weight: 700; }
.arr.tone-pos .arr-tone-icon { color: var(--ok); }
.arr.tone-neg .arr-tone-icon { color: var(--danger); }
.arr-del { background: transparent; color: var(--danger); }
.meta-item .meta-key { flex: 0 0 30%; }
.meta-item .meta-value { flex: 1; }

/* 折りたたみ */
details.adv {
  background: var(--surface-2);
  border: 1px solid var(--glass-border); border-radius: 12px; overflow: hidden;
}
details.adv > summary {
  list-style: none; cursor: pointer; padding: 12px 14px; font-size: var(--fs-sm); font-weight: 600; color: var(--muted);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  transition: color .15s var(--ease);
}
details.adv > summary:hover { color: var(--text); }
details.adv > summary::-webkit-details-marker { display: none; }
.adv-chevron { flex-shrink: 0; color: var(--accent); transition: transform .18s var(--ease); }
details.adv[open] > summary { color: var(--text); border-bottom: 1px solid var(--line); }
details.adv[open] > summary .adv-chevron { transform: rotate(180deg); }
details.adv > .adv-body { padding: 0 14px 14px; display: flex; flex-direction: column; gap: 12px; }
details.adv[open] > .adv-body { animation: advIn .15s var(--ease); }
@keyframes advIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }

.add-cut {
  width: 100%; border: 1px dashed var(--line); background: transparent; color: var(--accent);
  transition: border-color .15s var(--ease), background .15s var(--ease);
}
.add-cut:hover { border-color: var(--accent); background: var(--accent-bg); }

/* 保存ステータス */
.save-status {
  flex-shrink: 0; margin: 0 14px calc(14px + env(safe-area-inset-bottom)); padding: 12px 14px;
  border-radius: 12px; font-size: var(--fs-sm);
  backdrop-filter: blur(16px) saturate(150%);
  -webkit-backdrop-filter: blur(16px) saturate(150%);
  box-shadow: var(--shadow-md);
}
.save-status-head-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.save-status-head { font-weight: 700; }
.save-status-close {
  flex-shrink: 0; border-radius: 8px; background: transparent; color: inherit; opacity: .7;
}
.save-status-close:hover { opacity: 1; background: rgba(255, 255, 255, .1); filter: none; }
.save-status-body {
  margin: 8px 0 0; font-family: ui-monospace, monospace; font-size: var(--fs-xs);
  white-space: pre-wrap; word-break: break-word;
  max-height: 30vh; overflow-y: auto;
  background: rgba(0, 0, 0, .18); border-radius: 8px; padding: 8px 10px;
}
.save-status.ok { background: rgba(16, 36, 26, .85); border: 1px solid var(--ok); color: var(--ok); }
.save-status.warn { background: rgba(42, 36, 19, .85); border: 1px solid var(--warn); color: var(--warn); }
.save-status.err { background: rgba(42, 20, 20, .85); border: 1px solid var(--danger); color: var(--danger); }

.error { color: var(--danger); font-size: var(--fs-xs); margin: 0; min-height: 1em; }

/* トースト */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: rgba(15, 17, 24, .78); color: #fff; padding: 12px 20px; border-radius: 24px;
  font-size: var(--fs-sm); font-weight: 600; z-index: 100;
  border: 1px solid rgba(255, 255, 255, .1);
  box-shadow: var(--shadow-md);
  backdrop-filter: blur(14px) saturate(160%);
  -webkit-backdrop-filter: blur(14px) saturate(160%);
}

/* ライトテーマ */
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f2f3f7; --panel2: #e7eaf2; --line: #d7dae6;
    --panel-glass: rgba(255, 255, 255, .55); --panel-glass-strong: rgba(255, 255, 255, .72);
    --glass-border: rgba(20, 20, 40, .08); --glass-highlight: rgba(255, 255, 255, .6);
    --text: #1c1f2a; --muted: #626a80;
    --text-secondary: #3d4257; --text-strong: #2a2e3d;
    --surface: #fbfbfe; --surface-2: #f2f3f8;
    --accent: #d1650b; --accent-soft: #e9852f; --accent-press: #b5540a; --accent-ink: #fff;
    --accent-bg: rgba(209, 101, 11, .1);
    --accent2: #0e847d; --accent2-bg: rgba(14, 132, 125, .1);
    --danger: #c23327; --danger-bg: rgba(194, 51, 39, .1);
    --warn: #8a5a00; --warn-bg: rgba(138, 90, 0, .1);
    --ok: #1b7d4d; --ok-bg: rgba(27, 125, 77, .1);
    --shadow-sm: 0 1px 2px rgba(20, 20, 40, .08);
    --shadow-md: 0 1px 1px rgba(20, 20, 40, .05), 0 10px 24px rgba(20, 20, 40, .08), 0 28px 56px rgba(20, 20, 40, .06);
    --shadow-glow: 0 10px 28px rgba(209, 101, 11, .25);
  }
  body::before, body::after { opacity: .13; }
  .card-arrow { color: var(--accent); }
  .note { background: rgba(0, 0, 0, .035); }
  .toast { background: rgba(28, 31, 42, .82); }
  .save-status.ok { background: rgba(232, 247, 238, .9); } .save-status.warn { background: rgba(253, 246, 227, .9); } .save-status.err { background: rgba(253, 236, 236, .9); }
}

/* --- 透過を抑えたい環境: 残したガラス（chrome）を不透明にフォールバック --- */
@media (prefers-reduced-transparency: reduce) {
  .bar, .editor-subbar, .login-box, .toast, .save-status {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  .bar, .editor-subbar, .login-box { background: var(--surface-2); }
  .toast { background: rgb(15, 17, 24); }
  .save-status.ok { background: rgb(16, 36, 26); }
  .save-status.warn { background: rgb(42, 36, 19); }
  .save-status.err { background: rgb(42, 20, 20); }
}
@media (prefers-color-scheme: light) and (prefers-reduced-transparency: reduce) {
  .toast { background: rgb(28, 31, 42); }
  .save-status.ok { background: rgb(232, 247, 238); }
  .save-status.warn { background: rgb(253, 246, 227); }
  .save-status.err { background: rgb(253, 236, 236); }
}

/* --- 高コントラスト環境: border を強め、muted系テキストを1段濃くする --- */
@media (prefers-contrast: more) {
  :root {
    --line: #3c4256; --glass-border: rgba(255, 255, 255, .18);
    --muted: #aab1c4; --text-secondary: #cdd1e0;
  }
}
@media (prefers-color-scheme: light) and (prefers-contrast: more) {
  :root {
    --line: #b7bcce; --glass-border: rgba(20, 20, 40, .18);
    --muted: #4b5268; --text-secondary: #2c3040;
  }
}
