:root {
  color-scheme: light dark;

  --bg: #ffffff;
  --bg-bar: #f2f3f5;
  --bg-raised: #ffffff;
  --fg: #16181d;
  --fg-muted: #5d6470;
  --border: #d7dae0;
  --accent: #2f6df6;
  --accent-fg: #ffffff;
  --danger: #c0362c;
  --warn-bg: #fff5e6;
  --warn-fg: #7a4b00;

  --bar-h: 44px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181d;
    --bg-bar: #1f2228;
    --bg-raised: #23262d;
    --fg: #e8eaee;
    --fg-muted: #99a1af;
    --border: #343943;
    --accent: #6f9bff;
    --accent-fg: #10131a;
    --danger: #ff8177;
    --warn-bg: #3a2f1c;
    --warn-fg: #f0cf9a;
  }
}

* { box-sizing: border-box; }

/*
 * .banner などは display を持つため、UA の [hidden]{display:none} に勝ってしまう。
 * hidden 属性での出し分けを全要素で確実にするために強制する。
 */
[hidden] { display: none !important; }

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

body {
  /*
   * ソフトキーボードが出ても 100dvh は縮まないため、アクションバーがキーボードの下へ
   * 隠れてしまう。app.js が visualViewport の実測値を --app-h に入れるので、それを優先し、
   * 取れない環境では 100dvh（モバイルのURLバー伸縮に追従する）へ落とす。
   * position:fixed は、キーボード表示時に iOS が画面ごとスクロールさせるのを防ぐため。
   */
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: var(--app-h, 100dvh);
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Segoe UI", sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---- バー ---- */

.bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-bar);
  border-color: var(--border);
  border-style: solid;
  border-width: 0;
}

.bar-top {
  justify-content: space-between;
  min-height: var(--bar-h);
  padding: 0 10px;
  border-bottom-width: 1px;
  padding-top: env(safe-area-inset-top, 0px);
}

.bar-bottom {
  border-top-width: 1px;
  padding-bottom: var(--safe-b);
}

/* キーボードが出ている間はホームインジケータ領域がキーボードに隠れるので、その余白を詰める */
body.keyboard-open .bar-bottom { padding-bottom: 0; }

.counts {
  font-size: 12px;
  color: var(--fg-muted);
  font-variant-numeric: tabular-nums;
}

.count-selection { color: var(--accent); }

.bar-actions { display: flex; gap: 4px; }

/* ---- 編集エリア ---- */

.editor-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

.editor {
  flex: 1 1 auto;
  width: 100%;
  padding: 12px 12px 24px;
  border: 0;
  resize: none;
  background: var(--bg);
  color: var(--fg);
  /* iOSは16px未満の入力欄でズームするため、16pxを下回らせない */
  font: 16px/1.7 -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.editor:focus { outline: none; }
.editor::placeholder { color: var(--fg-muted); }

/* ---- アクションバー ---- */

.action-bar {
  display: flex;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  overflow-x: auto;
  scrollbar-width: none;
}

.action-bar::-webkit-scrollbar { display: none; }

.action-btn {
  flex: 0 0 auto;
  min-height: 38px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 19px;
  background: var(--bg-raised);
  color: var(--fg);
  font-size: 14px;
  white-space: nowrap;
  cursor: pointer;
}

.action-btn:active { background: var(--bg-bar); }

.action-btn[disabled] {
  opacity: 0.45;
  cursor: not-allowed;
}

.action-bar-empty {
  padding: 10px 4px;
  color: var(--fg-muted);
  font-size: 13px;
}

/* action-bar-user はレイアウトの箱を持たず、中の子要素がそのまま親のflex行に並ぶ */
.action-bar-user {
  display: contents;
}

.action-btn-edit {
  color: var(--fg-muted);
}

.action-bar-sep {
  flex: 0 0 auto;
  width: 1px;
  align-self: stretch;
  margin: 4px 2px;
  background: var(--border);
}

/* ---- ボタン ---- */

.icon-btn {
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--fg-muted);
  font-size: 13px;
  cursor: pointer;
}

.icon-btn:hover { color: var(--fg); border-color: var(--border); }

.primary-btn {
  min-height: 40px;
  padding: 0 16px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.danger-btn {
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--danger);
  font-size: 14px;
  cursor: pointer;
}

/* ---- トースト ---- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bar-h) + 24px + var(--safe-b));
  transform: translateX(-50%);
  max-width: min(90vw, 420px);
  padding: 10px 16px;
  border-radius: 8px;
  background: #16181d;
  color: #ffffff;
  font-size: 13px;
  text-align: center;
  z-index: 20;
}

@media (prefers-color-scheme: dark) {
  .toast { background: #3a3f49; }
}

/* ---- バナー ---- */

.banner {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--warn-bg);
  color: var(--warn-fg);
  font-size: 13px;
}

.banner-btn {
  padding: 4px 10px;
  border: 1px solid currentColor;
  border-radius: 6px;
  background: transparent;
  color: inherit;
  font-size: 13px;
  cursor: pointer;
}

.banner-btn-quiet {
  margin-left: auto;
  border-color: transparent;
}

/* ---- ダイアログ ---- */

.dialog {
  width: min(560px, 100vw);
  max-width: 100vw;
  max-height: 100dvh;
  padding: 0;
  border: 0;
  border-radius: 12px;
  background: var(--bg-raised);
  color: var(--fg);
}

.dialog::backdrop { background: rgb(0 0 0 / 45%); }

.dialog-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-raised);
}

.dialog-head h2 { margin: 0; font-size: 16px; }

.dialog-body { padding: 16px; }

.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px calc(12px + var(--safe-b));
  border-top: 1px solid var(--border);
}

.dialog-foot .danger-btn { margin-right: auto; }

/* ---- フォーム ---- */

.field {
  display: block;
  margin-bottom: 14px;
}

.field > span {
  display: block;
  margin-bottom: 4px;
  font-size: 13px;
  color: var(--fg-muted);
}

.field input,
.field select,
.field textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  font-family: inherit;
  /* iOSは16px未満の入力欄でフォーカス時にズームするので、16pxを下回らせない */
  font-size: 16px;
  line-height: 1.5;
}

.field textarea {
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.check {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--fg-muted);
}

.check input { margin-top: 2px; }

.error {
  margin: 0;
  padding: 8px 10px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
  font-size: 13px;
}

/* ---- 設定の一覧 ---- */

.action-list {
  margin: 0 0 14px;
  padding: 0;
  list-style: none;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.action-list:empty { display: none; }

.action-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}

.action-row:last-child { border-bottom: 0; }

.action-row-main {
  flex: 1 1 auto;
  min-width: 0;
}

.action-row-name {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.action-row-sub {
  font-size: 11px;
  color: var(--fg-muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-btn {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--fg-muted);
  font-size: 13px;
  cursor: pointer;
}

.row-btn[disabled] { opacity: 0.3; cursor: default; }

.sync-conflict-actions {
  display: flex;
  gap: 8px;
}

.dialog-divider {
  margin: 16px 0;
  border: 0;
  border-top: 1px solid var(--border);
}

/* ---- 注記 ---- */

.note {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--fg-muted);
}

.note-warn {
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--warn-bg);
  color: var(--warn-fg);
}

.help {
  margin-top: 16px;
  font-size: 13px;
  color: var(--fg-muted);
}

.help summary { cursor: pointer; }
.help ul { padding-left: 20px; }
.help li { margin: 6px 0; }

code {
  padding: 1px 4px;
  border-radius: 4px;
  background: var(--bg-bar);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
}
