/* 色はすべて 文字色・背景色・アクセント の3つから導く。app.js がこの3つを書き換える。 */
:root {
  --fg: #2b2620;
  --bg: #f7f3ea;
  --accent: #b5492c;
  --on-accent: #fff;

  --surface: color-mix(in srgb, var(--fg) 4%, var(--bg));
  --hover: color-mix(in srgb, var(--fg) 9%, var(--bg));
  --line: color-mix(in srgb, var(--fg) 15%, var(--bg));
  --muted: color-mix(in srgb, var(--fg) 60%, var(--bg));

  --font: "Hiragino Mincho ProN", "Yu Mincho", YuMincho, "Noto Serif JP", serif;
  --font-size: 18px;
  --line-height: 1.9;
  --letter-spacing: 0.03em;

  --ui-font: system-ui, -apple-system, "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", sans-serif;
}

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

html, body { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--fg);
  font: 13px/1.5 var(--ui-font);
  overflow: hidden;
}

#app {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100dvh;
}

/* ---------- 共通の部品 ---------- */
button, input, select { font: inherit; color: inherit; }

button {
  height: 30px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { background: var(--hover); }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
button.primary:hover { filter: brightness(1.08); }
button[aria-pressed="true"], button[aria-expanded="true"] {
  background: var(--hover);
  border-color: var(--muted);
}
button.icon { width: 30px; padding: 0; display: inline-grid; place-items: center; }
button.icon svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }

input[type="text"], input[type="search"], input[type="password"], select {
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  min-width: 0;
}

/* ---------- ツールバー ---------- */
.toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.toolbar .group { display: flex; gap: 6px; padding-left: 8px; border-left: 1px solid var(--line); }

#doc-name {
  flex: 1 1 160px;
  max-width: 360px;
  border-color: transparent;
  background: transparent;
  font-weight: 600;
  font-size: 14px;
}
#doc-name:hover { border-color: var(--line); }
#doc-name:focus { background: var(--bg); border-color: var(--line); }

#save-state {
  flex: 1 1 0; min-width: 4em; color: var(--muted); font-size: 12px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#save-state[data-kind="error"] { color: var(--accent); font-weight: 600; }

.segmented { display: flex; }
.segmented button { border-radius: 0; margin-left: -1px; }
.segmented button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.segmented button:last-child { border-radius: 0 6px 6px 0; }
.segmented button[aria-pressed="true"] { position: relative; }

/* ---------- 本体 ---------- */
.body { display: flex; min-height: 0; position: relative; }

#sidebar, #settings {
  flex: none;
  width: 250px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#sidebar { border-right: 1px solid var(--line); }
#settings { width: 280px; border-left: 1px solid var(--line); padding: 4px 16px 20px; overflow-y: auto; }
#sidebar[hidden], #settings[hidden] { display: none; }

.side-head { padding: 12px 12px 8px; display: grid; gap: 8px; }
.side-tools { display: flex; gap: 6px; padding: 0 12px 8px; }
.side-tools input { flex: 1; }

.field { display: grid; gap: 4px; }
.field > span { color: var(--muted); font-size: 12px; display: flex; justify-content: space-between; }
.field output { font-variant-numeric: tabular-nums; }

#provider-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
#provider-actions:empty { display: none; }
a.button {
  display: inline-flex; align-items: center; height: 30px; padding: 0 11px;
  border-radius: 6px; background: var(--accent); color: var(--on-accent); text-decoration: none;
}

#file-list { list-style: none; margin: 0; padding: 0 6px 12px; overflow-y: auto; flex: 1; }
#file-list li { display: flex; align-items: center; border-radius: 6px; }
#file-list li:hover { background: var(--hover); }
#file-list li[aria-current="true"] { background: color-mix(in srgb, var(--accent) 16%, var(--bg)); }
#file-list .open {
  flex: 1; min-width: 0; height: auto; padding: 6px 8px; border: 0; background: none;
  text-align: left; white-space: normal; overflow-wrap: anywhere; line-height: 1.35;
}
#file-list .dir { display: block; color: var(--muted); font-size: 11px; }
#file-list .delete { border: 0; width: 26px; height: 26px; padding: 0; color: var(--muted); opacity: 0; }
#file-list li:hover .delete, #file-list .delete:focus-visible { opacity: 1; }
#file-list .empty { padding: 16px 8px; color: var(--muted); display: block; }

/* ---------- 編集領域 ---------- */
#panes { flex: 1; min-width: 0; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); }
#panes[data-view="edit"] #preview,
#panes[data-view="preview"] #editor { display: none; }
#panes[data-view="split"] { grid-template-columns: repeat(2, minmax(0, 1fr)); }
#panes[data-view="split"] #preview { border-left: 1px solid var(--line); }

/* 縦書きは行が横へ伸びていくので、分割は上下に並べる */
[data-vertical="true"] #panes[data-view="split"] {
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: repeat(2, minmax(0, 1fr));
}
[data-vertical="true"] #panes[data-view="split"] #preview { border-left: 0; border-top: 1px solid var(--line); }

#editor, #preview {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 28px clamp(16px, 4vw, 48px);
  border: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: var(--font-size);
  line-height: var(--line-height);
  letter-spacing: var(--letter-spacing);
  font-feature-settings: normal;
  overflow: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-break: strict;
}
#editor { resize: none; display: block; outline-offset: -2px; caret-color: var(--accent); }
#editor::placeholder { color: var(--muted); opacity: 0.7; }
#editor::selection, #preview ::selection { background: color-mix(in srgb, var(--accent) 30%, var(--bg)); }

[data-vertical="true"] #editor,
[data-vertical="true"] #preview {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  padding: clamp(16px, 4vh, 36px) 28px;
}

#preview p { margin: 0; }
#preview rt { font-size: 0.5em; letter-spacing: 0; line-height: 1; user-select: none; }
#preview .emphasis { text-emphasis: filled dot; -webkit-text-emphasis: filled dot; }
[data-vertical="true"] #preview .emphasis { text-emphasis: filled sesame; -webkit-text-emphasis: filled sesame; }
#preview .tcy { text-combine-upright: all; -webkit-text-combine: horizontal; letter-spacing: 0; }

/* 横書きは1行が長くなりすぎないようにする */
[data-vertical="false"] #preview p { max-width: 42em; margin-inline: auto; }

/* ---------- 設定パネル ---------- */
#settings h2 {
  margin: 18px 0 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--muted);
}
#settings .field { margin-bottom: 10px; }
input[type="range"] { width: 100%; accent-color: var(--accent); margin: 0; }

.presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.presets button {
  height: 44px; padding: 0; font-size: 12px;
  background: var(--p-bg); color: var(--p-fg); border-color: var(--line);
  font-family: var(--font);
}
.presets button:hover { background: var(--p-bg); border-color: var(--muted); }
.presets button[aria-pressed="true"] { outline: 2px solid var(--accent); outline-offset: 1px; background: var(--p-bg); }

.colors { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.colors label { display: grid; gap: 4px; justify-items: center; font-size: 12px; color: var(--muted); cursor: pointer; }
.colors input { width: 100%; height: 34px; padding: 2px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); cursor: pointer; }

.check { display: flex; gap: 8px; align-items: flex-start; cursor: pointer; }
.check input { accent-color: var(--accent); margin-top: 3px; }

.help { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 12px; }
.help dt { font-family: ui-monospace, Menlo, monospace; white-space: nowrap; }
.help dd { margin: 0; color: var(--muted); }

/* ---------- ステータスバー ---------- */
.statusbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 4px 12px;
  border-top: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.statusbar span { min-width: 0; overflow: hidden; text-overflow: ellipsis; }

/* ---------- ダイアログ・通知 ---------- */
dialog {
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(0 0 0 / 0.35); }
dialog h2 { margin: 0 0 8px; font-size: 15px; }
dialog p { margin: 0 0 12px; color: var(--muted); overflow-wrap: anywhere; }
dialog p:empty { display: none; }
#dialog-fields { display: grid; gap: 10px; }
#dialog-fields input { width: 100%; height: 34px; font-size: 15px; }
.dialog-buttons { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; margin-top: 16px; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 44px;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  padding: 9px 14px;
  border-radius: 8px;
  background: var(--fg);
  color: var(--bg);
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.25);
  z-index: 10;
}

/* ---------- 狭い画面 ---------- */
@media (max-width: 760px) {
  #sidebar, #settings {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 5;
    width: min(300px, 86vw);
    box-shadow: 0 0 30px rgb(0 0 0 / 0.25);
  }
  #sidebar { left: 0; }
  #settings { right: 0; }
  .toolbar .group { padding-left: 0; border-left: 0; }
  #save-state { flex-basis: 100%; order: 10; }
  #panes[data-view="split"] { grid-template-columns: minmax(0, 1fr); grid-template-rows: repeat(2, minmax(0, 1fr)); }
  #panes[data-view="split"] #preview { border-left: 0; border-top: 1px solid var(--line); }
  #file-list .delete { opacity: 1; }
}

/* ---------- ログイン画面 ---------- */
.login-page { display: grid; place-items: center; height: 100dvh; padding: 16px; overflow: auto; }
.login-card {
  width: min(340px, 100%);
  display: grid;
  gap: 14px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}
.login-card h1 { margin: 0 0 4px; font: 600 22px var(--font); letter-spacing: 0.04em; }
.login-card input { height: 36px; font-size: 15px; }
.login-card input[type="password"] { padding: 0 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.login-card button { height: 38px; font-size: 14px; }
.login-card button:disabled { opacity: 0.6; cursor: default; }
#login-error { margin: 0; min-height: 1.5em; color: var(--accent); font-weight: 600; }

.note { margin: 0; color: var(--muted); font-size: 12px; }
a.button.plain { background: transparent; color: var(--fg); border: 1px solid var(--line); }
a.button.plain:hover { background: var(--hover); }
#account-name { margin: 0 0 8px; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
.account-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.login-card a.button { justify-content: center; height: 38px; }

/* ---------- 管理画面 ---------- */
.admin-page { overflow: auto; }
.admin-page .toolbar { gap: 14px; font-size: 14px; }
.admin-main { max-width: 860px; margin: 0 auto; padding: 8px 16px 48px; }
.admin-main section { margin-top: 28px; display: grid; gap: 10px; justify-items: start; }
.admin-main h2 { margin: 0; font-size: 15px; }
.link-box { width: 100%; display: grid; gap: 6px; padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.link-row { display: flex; gap: 6px; }
.link-row input { flex: 1; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.table-wrap { width: 100%; overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; }
.admin-main table { width: 100%; border-collapse: collapse; white-space: nowrap; }
.admin-main th, .admin-main td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--line); }
.admin-main th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface); }
.admin-main tr:last-child td { border-bottom: 0; }
.admin-main td.actions { display: flex; gap: 6px; justify-content: flex-end; }
.badge { margin-left: 8px; padding: 1px 7px; border: 1px solid var(--line); border-radius: 999px; font-size: 11px; color: var(--muted); }

.stack { width: 100%; max-width: 520px; display: grid; gap: 10px; }
.stack input[type="password"] { height: 30px; padding: 0 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.note a { color: var(--accent); }

/* 選択肢が多いダイアログは、ボタンを縦に並べる */
.dialog-buttons.stacked { flex-direction: column; align-items: stretch; max-height: 60vh; overflow-y: auto; }
.dialog-buttons.stacked button { height: auto; min-height: 34px; padding: 6px 11px; white-space: normal; text-align: left; }

/* ---------- 入力欄の説明と、情報の入手手順 ---------- */
.hint { color: var(--muted); font-size: 11.5px; line-height: 1.45; }
.guide { margin-bottom: 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.guide summary { padding: 8px 12px; cursor: pointer; font-weight: 600; }
.guide ol { margin: 0; padding: 2px 14px 12px 32px; display: grid; gap: 7px; color: var(--fg); line-height: 1.55; }
.guide a { color: var(--accent); white-space: nowrap; }
.guide pre {
  margin: 6px 0 0; padding: 8px 10px; overflow-x: auto; border-radius: 6px;
  background: var(--bg); border: 1px solid var(--line);
  font: 11px/1.5 ui-monospace, Menlo, Consolas, monospace; user-select: all;
}
.admin-main .guide { width: 100%; max-width: 620px; margin-bottom: 0; }
.guide code { font: 11.5px ui-monospace, Menlo, Consolas, monospace; }

.guide li { font-weight: 400; }
.field .hint { font-weight: 400; }
/* 手順の中のコード例が、ダイアログの幅を押し広げないようにする */
.guide ol { grid-template-columns: minmax(0, 1fr); }
.guide li { min-width: 0; overflow-wrap: anywhere; }
.guide pre { white-space: pre-wrap; overflow-wrap: anywhere; }
