:root {
  --bg: #f7f5ef;
  --board: #ffffff;
  --rule: #d9e2ec;
  --rule-strong: #b8c4d0;
  --ink: #1f2328;
  --muted: #6e7781;
  --accent: #0969da;
  --danger: #cf222e;
  --toolbar-h: 58px;
  --jp-font: 'Klee One', cursive;
  /* ホワイトボードは端末のダークモードに関係なく、いつも明るい配色 */
  color-scheme: light;
  --tr-font: 'Noto Sans', 'Noto Sans JP', sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--ink);
  font-family: system-ui, -apple-system, 'Hiragino Sans', sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* ---------- toolbar ---------- */
#toolbar {
  height: var(--toolbar-h);
  display: flex; align-items: center; gap: 6px;
  padding: 6px max(10px, env(safe-area-inset-right)) 6px max(10px, env(safe-area-inset-left));
  background: #fff; border-bottom: 1px solid var(--rule-strong);
  user-select: none; -webkit-user-select: none;
  overflow-x: auto; scrollbar-width: none;
}
#toolbar::-webkit-scrollbar { display: none; }
.group { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.spacer { flex: 1; }

button { font: inherit; color: var(--ink); cursor: pointer; touch-action: manipulation; }
.tool, .sub {
  min-width: 44px; height: 44px; padding: 0 8px;
  border: 1px solid transparent; border-radius: 10px; background: transparent;
  display: inline-flex; align-items: center; gap: 4px; font-size: 18px;
}
.tool span { font-size: 13px; }
.tool:hover, .sub:hover { background: #f0f2f4; }
.tool.active, .sub.active { background: #ddf4ff; border-color: var(--accent); color: #0550ae; }
.tool.danger { color: var(--danger); }
.tool.icon { font-size: 20px; justify-content: center; }
#shapeGroup { display: none; padding-left: 6px; border-left: 1px solid var(--rule); }
body[data-mode="shape"] #shapeGroup { display: flex; }

.swatch {
  width: 32px; height: 32px; border-radius: 50%; padding: 0;
  border: 3px solid #fff; background: var(--c); box-shadow: 0 0 0 1px var(--rule-strong);
  position: relative; overflow: hidden; flex-shrink: 0;
}
.swatch.active { box-shadow: 0 0 0 3px var(--ink); }
.swatch.custom { background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red); display: inline-block; }
.swatch.custom input { position: absolute; inset: -10px; width: 60px; height: 60px; opacity: 0; cursor: pointer; }

#langSelect { max-width: 130px; }
select {
  height: 40px; border-radius: 10px; border: 1px solid var(--rule-strong);
  background: #fff; padding: 0 8px; font-size: 15px; color: var(--ink);
}

/* ---------- board ---------- */
#board {
  position: relative;
  height: calc(100% - var(--toolbar-h));
  margin: 0; background: var(--board);
  overflow: hidden;
}
/* 表示行数ぶんが画面に収まり、それより下の行はスクロールで表示 */
#scroller {
  position: absolute; inset: 0;
  overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
#sheet { position: relative; }
#rows { display: flex; flex-direction: column; }

.row {
  position: relative; height: var(--row-h, 190px); flex: none;
  border-bottom: 2px dashed var(--rule);
  display: flex;
}
.row-no {
  width: 44px; flex-shrink: 0; display: flex; flex-direction: column; gap: 4px;
  align-items: center; justify-content: center; cursor: pointer;
  color: var(--muted); font-size: 15px; border-right: 1px solid var(--rule);
  background: #fafbfc;
  user-select: none; -webkit-user-select: none;
  touch-action: pan-y;  /* 行番号の列は指でなぞるとスクロール */
}
body.no-lineno .row-no { width: 14px; font-size: 0; }
body.no-lineno .row-no .eye { display: none; }
.row-no .eye {
  width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; opacity: .45;
}
.row-no .eye .slash { display: none; }
.row-no:hover { background: #eef2f6; }
.row-no:hover .eye { opacity: .9; }

/* 畳んだ行：細い帯にして、番号をアイコン（チップ）で表示 */
.collapsed-label { display: none; }
.row.collapsed { height: 34px; background: repeating-linear-gradient(135deg, #f6f8fa 0 8px, #eef1f4 8px 16px); }
.row.collapsed .row-body { display: none; }
.row.collapsed .row-no { flex-direction: row; gap: 3px; background: transparent; border-right: none; width: auto; padding: 0 6px 0 8px; }
.row.collapsed .row-no .num {
  min-width: 26px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: #57606a; color: #fff; font-size: 13px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
}
.row.collapsed .row-no .eye { opacity: .7; width: 16px; height: 16px; }
.row.collapsed .row-no .eye .slash { display: inline; }
body.no-lineno .row.collapsed .row-no { font-size: 13px; }
body.no-lineno .row.collapsed .row-no .eye { display: inline; }
.row.collapsed .collapsed-label {
  display: flex; align-items: center; flex: 1; padding-left: 8px; cursor: pointer;
  color: #8c959f; font-size: 13px; user-select: none; -webkit-user-select: none;
}
.row-body {
  position: relative; flex: 1; min-width: 0;
  display: flex; flex-direction: column; justify-content: center;
  padding: 0 72px 0 18px;
}
body[data-mode="write"] .row-body { touch-action: none; }
body[data-mode="write"].pen-only .row-body { touch-action: pan-y; }

.jp {
  font-family: var(--jp-font); font-weight: 600;
  font-size: var(--jp-size, 48px); line-height: 1.25;
  white-space: nowrap; overflow: hidden;
  min-height: 1.25em; outline: none;
  color: var(--ink);
  border-radius: 6px;
}
body:not(.no-furi) .jp { padding-top: .5em; }
.jp ruby { ruby-position: over; }
.jp rt {
  font-size: .42em; font-weight: 500; line-height: 1;
  letter-spacing: .02em; opacity: .85;
  user-select: none; -webkit-user-select: none;
}
.ro, .tr {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  user-select: none; -webkit-user-select: none;
}
.ro {
  font-family: var(--tr-font); font-style: italic;
  font-size: var(--ro-size, 22px); line-height: 1.3; min-height: 1.3em;
  color: #0a6b6b;
}
.tr {
  font-family: var(--tr-font);
  font-size: var(--tr-size, 22px); line-height: 1.3; min-height: 1.3em;
  color: #57606a;
}
.ro.loading, .tr.loading { color: #afb8c1; }
.tr.error { color: var(--danger); font-size: 14px; cursor: pointer; text-decoration: underline dotted; }
body.no-trans .tr { display: none; }
body.no-romaji .ro { display: none; }

.ink {
  position: absolute; inset: 0; width: 100%; height: 100%;
  z-index: 2; pointer-events: none;
}
body[data-mode="write"] .jp, body[data-mode="write"] .tr { user-select: none; -webkit-user-select: none; }

body[data-mode="edit"] .jp { background: #f6f8fa; box-shadow: inset 0 0 0 1px var(--rule); cursor: text; padding: 0 6px; }
/* キーボード入力モード：入力中の行だけ入力欄として表示 */
body[data-mode="type"] .row-body { cursor: text; }
body[data-mode="type"] .row.typing { background: #fffdf0; }
body[data-mode="type"] .row.typing .jp {
  background: #fff8c5; box-shadow: inset 0 0 0 2px #d4a72c; padding: 0 6px; cursor: text;
}
body[data-mode="type"] .row.typing .jp:empty::before { content: 'ここにキーボードで入力'; color: #b7a76a; font-weight: 400; }
body[data-mode="edit"] .jp:focus { background: #fff8c5; box-shadow: inset 0 0 0 2px #d4a72c; }

.row-actions {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; gap: 6px; z-index: 3;
}
.row-actions button {
  width: 48px; height: 40px; border-radius: 10px;
  border: 1px solid var(--rule-strong); background: #fff; font-size: 18px;
  opacity: .55;
}
.row-actions button:hover { opacity: 1; }
.row.empty .row-actions { display: none; }

/* 行の並べ替え */
.row-actions .grip {
  cursor: grab; touch-action: none; font-size: 22px; line-height: 1; color: var(--muted);
  user-select: none; -webkit-user-select: none;
}
body.row-dragging, body.row-dragging * { cursor: grabbing !important; user-select: none; -webkit-user-select: none; }
.row.dragging {
  z-index: 20; background: #fff;
  box-shadow: 0 10px 30px rgba(0,0,0,.22); border-radius: 8px; transition: none;
}
.row.dragging .grip { opacity: 1; background: #ddf4ff; border-color: var(--accent); }
#dropLine {
  position: absolute; left: 0; right: 0; height: 4px; z-index: 21;
  background: var(--accent); border-radius: 2px; pointer-events: none;
  box-shadow: 0 0 0 3px rgba(9,105,218,.18);
}
#dropLine[hidden] { display: none; }
.row.just-moved { animation: moved .7s ease-out; }
@keyframes moved { from { background: #ddf4ff; } to { background: transparent; } }
@media (prefers-reduced-motion: reduce) { .row.just-moved { animation: none; } }
.row.pending .jp::after {
  content: '…'; color: var(--muted); margin-left: 6px;
}

#scrollBtns {
  position: absolute; z-index: 8;
  /* 行番号の列の下に置く（行の削除ボタンと重ならない） */
  left: 2px; bottom: max(8px, env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; gap: 6px;
}
#scrollBtns button {
  width: 40px; height: 44px; border-radius: 10px; padding: 0;
  border: 1px solid var(--rule-strong); background: rgba(255,255,255,.92);
  font-size: 16px; color: var(--muted);
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}
#scrollBtns button:active { background: #ddf4ff; }

/* ---------- shapes ---------- */
#shapes {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 5; touch-action: none;
}
body.pen-only #shapes { touch-action: pan-y; }
body[data-mode="shape"] #shapes, body[data-mode="erase"] #shapes { pointer-events: all; }
#shapes .hit { stroke: transparent; fill: none; stroke-width: 26; pointer-events: none; }
body[data-mode="erase"] #shapes .hit { pointer-events: stroke; cursor: pointer; }
body[data-mode="erase"] #shapes g:hover .vis { opacity: .4; }

/* ---------- candidates ---------- */
#candidates {
  /* ツールバーの上に重ねて表示（書く場所をふさがない） */
  position: fixed; z-index: 20; top: 0; left: 0; right: 0;
  height: var(--toolbar-h);
  display: flex; align-items: center; gap: 8px;
  padding: 6px max(10px, env(safe-area-inset-right)) 6px max(10px, env(safe-area-inset-left));
  background: #fffbe6; border-bottom: 2px solid #d4a72c;
  box-shadow: 0 4px 14px rgba(0,0,0,.12);
}
#candidates[hidden] { display: none; }
.cand-label { font-size: 13px; color: var(--muted); flex-shrink: 0; }
.cand-list { flex: 1; min-width: 0; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.cand-ok { color: #1a7f37; }
.cand-list button, .cand-close, .cand-ok {
  height: 44px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--rule); background: #f6f8fa;
  font-family: var(--jp-font); font-size: 22px; white-space: nowrap; flex-shrink: 0;
}
.cand-list button.active { background: #ddf4ff; border-color: var(--accent); }
.cand-close, .cand-ok { font-family: inherit; font-size: 14px; color: var(--danger); }
.cand-ok { color: #1a7f37; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: rgba(31,35,40,.92); color: #fff; padding: 10px 18px; border-radius: 12px;
  z-index: 30; font-size: 15px; max-width: 90vw;
}
#toast[hidden] { display: none; }

/* ---------- settings ---------- */
dialog {
  border: none; border-radius: 16px; padding: 0; width: min(520px, 92vw);
  background: #fff; color: var(--ink); color-scheme: light;
  box-shadow: 0 12px 40px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.3); }
dialog form { padding: 20px 22px; display: flex; flex-direction: column; gap: 14px; }
dialog h2 { margin: 0; font-size: 20px; }
dialog label { display: flex; flex-direction: column; gap: 6px; font-size: 15px; }
dialog label.check { flex-direction: row; align-items: center; gap: 10px; }
dialog input[type="checkbox"] { width: 22px; height: 22px; }
dialog .hint { font-size: 13px; color: var(--muted); line-height: 1.6; margin: 0; }
dialog .actions { display: flex; justify-content: flex-end; gap: 10px; }
#copyArea {
  width: 100%; font: 14px/1.5 var(--tr-font); color: var(--ink); background: #f6f8fa;
  border: 1px solid var(--rule-strong); border-radius: 10px; padding: 10px; resize: vertical;
}
dialog .msg { margin: 0; font-size: 16px; line-height: 1.6; }
dialog .secondary {
  height: 42px; padding: 0 18px; border-radius: 10px;
  border: 1px solid var(--rule-strong); background: #fff; color: var(--ink); font-size: 15px;
}
dialog .primary.danger-btn { background: var(--danger); }
dialog .primary {
  height: 42px; padding: 0 22px; border-radius: 10px; border: none;
  background: var(--accent); color: #fff; font-size: 15px;
}

/* ---------- マーカー ---------- */
#markerBtn { position: relative; margin-left: 4px; }
#markerBtn .mk-icon { font-weight: 400; }
#markerBtn::after {
  content: ""; position: absolute; left: 8px; right: 8px; bottom: 5px; height: 5px;
  border-radius: 3px; background: var(--mk, #fff176);
}
#markerMenu {
  position: fixed; z-index: 25; display: flex; gap: 6px; padding: 8px;
  background: #fff; border: 1px solid var(--rule-strong); border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0,0,0,.15);
}
#markerMenu button {
  height: 40px; padding: 0 12px; border-radius: 8px; font-size: 14px;
  border: 1px solid var(--rule); background: var(--mk, #fff); color: var(--ink);
}
#markerMenu[hidden] { display: none; }
#markerMenu .mk-clear { background: #fff; color: var(--danger); }

/* ---------- 全画面 ---------- */
.fs-icon { font-weight: 400; font-size: 20px; }
/* 全画面 API が使えない画面では、ツールバーを隠してボードを広げる */
body.wide #toolbar { display: none; }
body.wide #board { height: 100%; }
#showToolbarBtn {
  position: fixed; z-index: 15;
  top: max(8px, env(safe-area-inset-top, 0px)); right: max(8px, env(safe-area-inset-right, 0px));
  height: 36px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--rule-strong); background: rgba(255,255,255,.92); color: var(--muted);
  font-size: 13px; box-shadow: 0 2px 8px rgba(0,0,0,.08);
}
:fullscreen, ::backdrop { background: var(--bg); }

/* iPad の横向き（約1180px）でツールバーが1列に収まるよう、補助ボタンは絵だけにする */
@media (max-width: 1300px) {
  .swatch { width: 28px; height: 28px; }
  #colorGroup { gap: 3px; }
  #langSelect { max-width: 112px; }
  #copyBtn span, #saveBtn span, #loadBtn span, #fullscreenBtn span, #markerBtn span,
  #modeGroup [data-mode="shape"] span, #modeGroup [data-mode="erase"] span, #clearAllBtn span { display: none; }
}
@media (max-width: 1100px) {
  .tool, .sub { min-width: 40px; padding: 0 6px; }
  .tool span { display: none; }
}
