:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0efe9;
  --text: #1f2421;
  --muted: #636b66;
  --border: #dedcd4;
  --accent: #2f7d4a;          /* モードごとに上書き */
  --accent-soft: #e5f1e8;
  --warn-bg: #fff4d6;
  --warn-border: #e0a800;
  --warn-text: #5c4300;
  --danger-bg: #fde8e6;
  --danger-border: #d6453a;
  --danger-text: #6b1a14;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141715;
    --surface: #1c201e;
    --surface-2: #232825;
    --text: #e8ebe9;
    --muted: #9aa39e;
    --border: #323935;
    --accent-soft: #1f3327;
    --warn-bg: #3a2f10;
    --warn-border: #c99a1e;
    --warn-text: #f6e2a8;
    --danger-bg: #3d1d1a;
    --danger-border: #e0665c;
    --danger-text: #f8cdc8;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}

/* モードカラー */
body[data-mode="new"]       { --accent: #2f7d4a; --accent-soft: color-mix(in srgb, #2f7d4a 14%, var(--surface)); }
body[data-mode="non"]       { --accent: #1f6fb2; --accent-soft: color-mix(in srgb, #1f6fb2 14%, var(--surface)); }
body[data-mode="cancel"]    { --accent: #b4532a; --accent-soft: color-mix(in srgb, #b4532a 14%, var(--surface)); }
body[data-mode="inbound"]   { --accent: #7a4fb5; --accent-soft: color-mix(in srgb, #7a4fb5 14%, var(--surface)); }
body[data-mode="follow"]    { --accent: #0f8a8a; --accent-soft: color-mix(in srgb, #0f8a8a 14%, var(--surface)); }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "BIZ UDPGothic", "Yu Gothic UI", "Meiryo", "Hiragino Sans", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  display: flex;
  flex-direction: column;
}
kbd {
  font-family: inherit;
  font-size: 11px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 5px;
  background: var(--surface);
  color: var(--muted);
}

/* ヘッダー・モードタブ */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 3px solid var(--accent);
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 16px; letter-spacing: .02em; white-space: nowrap; }
.modes { display: flex; gap: 6px; flex-wrap: wrap; }
.mode-tab {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 14px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.mode-tab:hover { background: var(--surface-2); }
.mode-tab .num { font-size: 11px; color: var(--muted); }
.mode-tab[aria-selected="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
}
.mode-tab[aria-selected="true"] .num { color: rgba(255,255,255,.8); }

/* 検索バー */
.searchbar { padding: 14px 20px 10px; }
.search-wrap { position: relative; max-width: 980px; }
.search-icon {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  width: 20px; height: 20px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round;
}
#q {
  width: 100%;
  font: inherit;
  font-size: 18px;
  padding: 12px 44px 12px 44px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
#q:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.search-wrap .hint { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); }
.scope { margin-top: 6px; font-size: 12px; color: var(--muted); }
.scope code {
  background: var(--surface-2); border-radius: 4px; padding: 1px 6px; font-size: 12px;
  font-family: ui-monospace, Consolas, monospace;
}

/* 2カラム */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
  gap: 16px;
  padding: 0 20px 12px;
}
.results, .detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  min-height: 0;
  overflow: auto;
}
.results-head {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface);
  padding: 10px 14px;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}
.result-list { list-style: none; margin: 0; padding: 6px; }
.group-label {
  font-size: 11px; font-weight: 700; color: var(--muted);
  padding: 10px 8px 4px; letter-spacing: .05em;
}
.result {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  color: inherit;
  background: none;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
}
.result:hover { background: var(--surface-2); }
.result[aria-current="true"] { background: var(--accent-soft); border-color: var(--accent); }
.result .r-title { font-weight: 700; font-size: 14px; line-height: 1.45; }
.result .r-meta { display: flex; gap: 6px; align-items: center; margin-top: 3px; font-size: 11px; color: var(--muted); }
.badge {
  display: inline-block;
  font-size: 11px;
  border-radius: 4px;
  padding: 0 6px;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.badge.mode { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.badge.ng { background: var(--danger-bg); color: var(--danger-text); font-weight: 700; }
.empty { padding: 24px 16px; color: var(--muted); font-size: 14px; }

/* 詳細 */
.detail { padding: 20px 24px 28px; }
.d-head { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 6px; }
.d-title { font-size: 20px; margin: 0 0 14px; line-height: 1.4; }

.script {
  position: relative;
  background: var(--accent-soft);
  border-left: 6px solid var(--accent);
  border-radius: 10px;
  padding: 16px 20px 18px;
  margin-bottom: 14px;
}
.script-label {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12px; font-weight: 700; color: var(--accent); margin-bottom: 6px; letter-spacing: .05em;
}
.script-text { font-size: 21px; line-height: 1.75; font-weight: 500; white-space: pre-wrap; }
.copy-btn {
  appearance: none; font: inherit; font-size: 12px;
  border: 1px solid var(--accent); color: var(--accent); background: var(--surface);
  border-radius: 6px; padding: 2px 10px; cursor: pointer;
}
.copy-btn:hover { background: var(--accent); color: #fff; }
.copy-btn.done { background: var(--accent); color: #fff; }

.notes {
  background: var(--warn-bg);
  border: 2px solid var(--warn-border);
  color: var(--warn-text);
  border-radius: 10px;
  padding: 12px 16px;
  margin-bottom: 18px;
  font-size: 16px;
  font-weight: 700;
  display: flex; gap: 10px; align-items: flex-start;
  white-space: pre-wrap;
}
.notes.danger { background: var(--danger-bg); border-color: var(--danger-border); color: var(--danger-text); }
.notes .icon { flex: none; font-size: 18px; line-height: 1.4; }

.section-label { font-size: 12px; font-weight: 700; color: var(--muted); margin: 18px 0 6px; letter-spacing: .05em; }
.answer { white-space: pre-wrap; font-size: 15px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  appearance: none; font: inherit; font-size: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 2px 10px; cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.source { font-size: 12px; color: var(--muted); margin-top: 18px; }

.foot {
  display: flex; flex-wrap: wrap; gap: 16px;
  padding: 6px 20px 10px; font-size: 12px; color: var(--muted);
}
.foot .built { margin-left: auto; }

mark { background: color-mix(in srgb, var(--warn-border) 35%, transparent); color: inherit; border-radius: 2px; }

@media (max-width: 820px) {
  body { height: auto; }
  .layout { grid-template-columns: 1fr; padding: 0 12px 12px; }
  .topbar, .searchbar, .foot { padding-left: 12px; padding-right: 12px; }
  .results { max-height: 40vh; }
  .script-text { font-size: 18px; }
}

/* ── アカウント・SV ─────────────────────────────── */
.account { margin-left: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.me { font-size: 13px; color: var(--muted); }
.btn-sv, .btn-ghost, .btn-mini {
  appearance: none; font: inherit; cursor: pointer; border-radius: 8px;
}
.btn-sv {
  border: 2px solid var(--sv); background: var(--surface); color: var(--sv);
  font-weight: 700; font-size: 14px; padding: 5px 14px;
}
.btn-sv:hover, .btn-sv.on { background: var(--sv); color: #fff; }
.btn-ghost { border: 1px solid var(--border); background: none; color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 999px; }
.btn-ghost:hover { color: var(--text); }
.btn-mini {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-size: 13px; padding: 4px 12px;
}
.btn-mini:hover { border-color: var(--accent); color: var(--accent); }
.btn-mini.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
.btn-mini.primary:hover { filter: brightness(1.08); color: #fff; }
.btn-mini.danger { border-color: var(--danger-border); color: var(--danger-text); }
.btn-mini.danger[data-armed="1"] { background: var(--danger-border); color: #fff; }
.btn-mini:disabled { opacity: .6; cursor: default; }

:root { --sv: #8a3fd1; --sv-soft: color-mix(in srgb, #8a3fd1 12%, var(--surface)); }
.sv-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  margin: 0; padding: 8px 20px;
  background: var(--sv-soft); border-bottom: 1px solid color-mix(in srgb, var(--sv) 40%, transparent);
  font-size: 13px;
}
.sv-bar strong { color: var(--sv); }
.sv-bar-actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.r-id { margin-left: auto; font-family: ui-monospace, Consolas, monospace; }
.d-actions { margin-left: auto; display: flex; gap: 6px; }
.muted { color: var(--muted); font-size: 13px; }

/* 過去の版 */
.history { margin-top: 22px; border-top: 1px dashed var(--border); padding-top: 4px; }
.hist-item { border: 1px solid var(--border); border-radius: 8px; margin: 6px 0; background: var(--surface-2); }
.hist-item summary { cursor: pointer; padding: 6px 12px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 13px; }
.hist-title { font-weight: 700; }
.hist-body { padding: 4px 12px 12px; font-size: 13px; display: grid; gap: 4px; white-space: pre-wrap; }
.hist-body b { color: var(--muted); font-size: 12px; }
.hist-body .btn-mini { justify-self: start; margin-top: 6px; }
.hist-body .btn-mini[data-armed="1"] { background: var(--accent); color: #fff; border-color: var(--accent); }

/* 編集フォーム */
.edit-form { display: grid; gap: 12px; }
.edit-form .d-title { margin-bottom: 4px; }
.edit-form label { display: grid; gap: 4px; font-size: 13px; font-weight: 700; color: var(--muted); }
.edit-form .help { font-weight: 400; font-size: 12px; }
.edit-form input, .edit-form select, .edit-form textarea {
  font: inherit; font-size: 15px; font-weight: 400; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px;
  width: 100%;
}
.edit-form textarea { resize: vertical; line-height: 1.6; }
.edit-form input[readonly] { background: var(--surface-2); color: var(--muted); }
.edit-form :is(input, select, textarea):focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.edit-form .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; }
.form-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; position: sticky; bottom: -28px; background: var(--surface); padding: 10px 0 4px; }
.form-msg { color: var(--danger-text); font-size: 13px; font-weight: 700; }
.form-warn { background: var(--warn-bg); border: 1px solid var(--warn-border); color: var(--warn-text); border-radius: 8px; padding: 8px 12px; margin: 0; font-size: 13px; }

/* トースト */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 40;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  font-size: 14px; box-shadow: var(--shadow); max-width: calc(100% - 32px);
}

/* ログイン */
.overlay { position: fixed; inset: 0; z-index: 50; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 24px; display: grid; gap: 12px; box-shadow: var(--shadow); }
.login-card h1 { font-size: 20px; margin: 0; }
.login-card p { margin: 0; color: var(--muted); font-size: 13px; }
.login-card label { display: grid; gap: 4px; font-size: 13px; font-weight: 700; color: var(--muted); }
.login-card input { font: inherit; border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; background: var(--bg); color: var(--text); font-size: 15px; }
.login-card button { font: inherit; background: #2f7d4a; color: #fff; border: 0; border-radius: 8px; padding: 10px; font-weight: 700; font-size: 15px; cursor: pointer; }
.login-card button:disabled { opacity: .6; }
.login-msg { color: var(--danger-text); font-size: 13px; font-weight: 700; min-height: 1em; }

/* 利用者の管理 */
.users { display: grid; gap: 8px; margin-top: 12px; }
.user-row {
  display: grid; grid-template-columns: minmax(180px, 1fr) auto auto; gap: 12px; align-items: center;
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--surface);
}
.user-who { display: grid; gap: 2px; min-width: 0; }
.user-who .muted { overflow-wrap: anywhere; }
.user-name {
  font: inherit; font-weight: 700; font-size: 15px; color: var(--text);
  border: 1px solid transparent; border-radius: 6px; padding: 2px 6px; margin-left: -6px; background: none; width: 100%;
}
.user-name:hover { border-color: var(--border); }
.user-name:focus { outline: none; border-color: var(--accent); background: var(--surface); }
.user-role { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg { display: inline-flex; align-items: center; padding: 5px 12px; font-size: 13px; cursor: pointer; color: var(--muted); }
.seg + .seg { border-left: 1px solid var(--border); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg.on { background: var(--sv-soft); color: var(--sv); font-weight: 700; }
.seg:has(input:disabled) { cursor: default; }
.seg:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }
@media (max-width: 820px) { .user-row { grid-template-columns: 1fr; } }

/* 利用者の追加・パスワード発行 */
.mono { font-family: ui-monospace, Consolas, monospace; font-weight: 700; color: var(--text); }
.user-row { grid-template-columns: minmax(180px, 1fr) auto auto; }
.user-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.add-user { border: 1px dashed var(--border); border-radius: 10px; padding: 8px 12px; margin: 12px 0; }
.add-user summary { cursor: pointer; font-weight: 700; color: var(--sv); }
.add-user-form { display: grid; gap: 10px; margin-top: 10px; }
.add-user-form label, .reset-box label { display: grid; gap: 4px; font-size: 13px; font-weight: 700; color: var(--muted); }
.add-user-form .help, .reset-box .help { font-weight: 400; font-size: 12px; }
.add-user-form input:not([type="radio"]), .reset-box input {
  font: inherit; font-size: 15px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; max-width: 420px;
}
.add-user-form input:not([type="radio"]):focus, .reset-box input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.add-user-form .user-role { justify-self: start; }
.reset-box { grid-column: 1 / -1; display: grid; gap: 8px; padding: 10px 12px; border-radius: 10px; background: var(--sv-soft); }
.reset-box[hidden] { display: none !important; }
.form-actions.plain { position: static; padding: 0; background: none; }
.issued {
  border: 2px solid var(--sv); background: var(--sv-soft); border-radius: 12px;
  padding: 14px 16px; margin: 10px 0 14px; display: grid; gap: 10px;
}
.issued-head { font-weight: 700; color: var(--sv); }
.issued-grid { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; align-items: center; }
.issued-grid span { font-size: 13px; color: var(--muted); font-weight: 700; }
.issued-grid code {
  font-family: ui-monospace, Consolas, monospace; font-size: 20px; letter-spacing: .06em;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 4px 10px; justify-self: start;
}
.issued p { margin: 0; font-size: 13px; }
@media (max-width: 820px) { .user-actions { justify-content: flex-start; } }
