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

/* iPhoneの高速連打をdouble-tap zoomにしない。ピンチ拡大は全体では維持する。 */
html, body,
button, summary, .btn, .menu-item, .uline, #board-umi-lines-head {
  touch-action: manipulation;
}

:root {
  --board-green: #009063;
  --board-line: #006646;
  --best: #ffd400;
  --accent: #2b6cb0;
  --bg: #f2f4f7;
  --panel: #ffffff;
  --cell: 56px;
}

body {
  font-family: "Segoe UI", "Hiragino Sans", "Yu Gothic UI", Meiryo, sans-serif;
  background: var(--bg);
  color: #222;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  min-width: 0;
}

/* ---------- ヘッダー ---------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: #1e3a2f;
  color: #fff;
  flex-shrink: 0;
}
#topbar h1 { font-size: 18px; font-weight: 700; }
#topbar h1 .sub { font-size: 12px; font-weight: 400; opacity: .75; margin-left: 6px; }
#book-info {
  display: block;
  margin: 5px 6px 2px;
  padding: 8px 6px 2px;
  border-top: 1px solid #e2e7e4;
  color: #5b6862;
  font-size: 11px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}
#book-info.error { color: #c0392b; }

#app-menu { position: relative; order: 3; margin-left: auto; }
#app-menu summary {
  list-style: none;
  min-width: 42px;
  padding: 5px 10px;
  text-align: center;
  font-size: 18px;
}
#app-menu summary::-webkit-details-marker { display: none; }
#app-menu[open] summary { background: #e8f2ed; }
#app-menu-panel {
  position: absolute;
  z-index: 20;
  top: calc(100% + 7px);
  right: 0;
  width: 220px;
  padding: 6px;
  border: 1px solid #c8d0cc;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
}
#map-menu { position: relative; }
#map-menu summary {
  list-style: none;
  min-width: 42px;
  padding: 5px 10px;
  text-align: center;
  font-size: 18px;
}
#map-menu summary::-webkit-details-marker { display: none; }
#map-menu-panel {
  position: absolute;
  z-index: 20;
  top: calc(100% + 7px);
  right: 0;
  width: 210px;
  padding: 6px;
  border: 1px solid #c8d0cc;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
}
.menu-item {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: #1e3a2f;
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover { background: #e8f2ed; }

.btn {
  display: inline-block;
  background: #ffffff;
  color: #1e3a2f;
  border: 1px solid #c8d0cc;
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}
.btn:hover { background: #e8f2ed; }
.btn.small { padding: 2px 8px; font-size: 12px; }
.file-btn input { display: none; }

/* ---------- レイアウト ---------- */
main {
  display: flex;
  justify-content: center;
  flex: 1;
  min-height: 0;
  gap: 16px;
  padding: 16px;
}

#board-pane {
  flex: 0 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
  position: relative;
}
#board-wrap, #game-status, #controls, #kifu, #hint { flex-shrink: 0; }

/* ---------- 盤面 ---------- */
#board-wrap {
  display: grid;
  grid-template-columns: 20px auto;
  grid-template-rows: 20px auto;
}
#col-labels {
  grid-column: 2;
  display: grid;
  grid-template-columns: repeat(8, var(--cell));
  align-items: center;
  justify-items: center;
  font-size: 12px;
  color: #555;
}
#row-labels {
  grid-row: 2;
  display: grid;
  grid-template-rows: repeat(8, var(--cell));
  align-items: center;
  justify-items: center;
  font-size: 12px;
  color: #555;
}
#board {
  grid-column: 2;
  grid-row: 2;
  display: grid;
  grid-template-columns: repeat(8, var(--cell));
  grid-template-rows: repeat(8, var(--cell));
  border: 3px solid #123528;
  background: var(--board-green);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.cell { touch-action: none; }
.cell {
  border: 1px solid var(--board-line);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.cell.cand, .cell.playable { cursor: pointer; }
.cell.cand:hover, .cell.playable:hover { background: rgba(255,255,255,.14); }

.disc {
  width: 82%;
  height: 82%;
  border-radius: 50%;
  position: relative;
}
.disc.black { background: radial-gradient(circle at 33% 30%, #4d4d4d, #0a0a0a 65%); }
.disc.white { background: radial-gradient(circle at 33% 30%, #ffffff, #cfcfcf 70%); border: 1px solid #aaa; }
.disc.last::after {
  content: "";
  position: absolute;
  inset: 38%;
  border-radius: 50%;
  background: #e5533d;
}

.score {
  font-size: 17px;
  font-weight: 700;
  color: #eaf7f1;
  text-shadow: 0 1px 2px rgba(0,0,0,.45);
}
.score.best { color: var(--best); font-size: 19px; }
.score.neg { color: #ffb3a7; }

.legal-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: rgba(255,255,255,.35);
}

/* ---------- ステータス・コントロール ---------- */
#game-status {
  display: flex;
  gap: 16px;
  align-items: center;
  font-size: 14px;
  min-height: 22px;
}
.disc-glyph {
  display: inline-block;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  vertical-align: -2px;
}
.disc-glyph.black { background: #111; }
.disc-glyph.white { background: #fff; border: 1px solid #888; }
#joseki-name { color: var(--accent); font-weight: 700; }

#controls { display: flex; gap: 8px; }
.btn.nav { font-size: 15px; padding: 4px 14px; }
#btn-copy-kifu { font-size: 12px; padding-inline: 4px; }

#kifu {
  font-size: 13px;
  font-family: Consolas, "Courier New", monospace;
  background: var(--panel);
  border: 1px solid #dde2e8;
  border-radius: 6px;
  padding: 6px 10px;
  max-width: calc(var(--cell) * 8 + 20px);
  word-break: break-all;
  min-height: 30px;
}
#hint { font-size: 11px; color: #778; max-width: calc(var(--cell) * 8 + 20px); }

#board-umi-lines {
  width: calc(var(--cell) * 8 + 20px);
  max-width: 100%;
  flex: 1 1 180px;
  min-height: 72px;
  overflow: hidden;
  border: 1px solid #ccd5d0;
  border-radius: 7px;
  background: #fff;
}
#board-umi-lines:not([hidden]) { display: flex; flex-direction: column; }
#board-umi-lines-head {
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-bottom: 1px solid #e1e6e3;
  background: #edf4f0;
  color: #234437;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  text-align: left;
  line-height: 1.45;
  cursor: pointer;
}
#board-umi-lines-head::after {
  content: "タップで拡大";
  float: right;
  color: #64756d;
  font-size: 10px;
  font-weight: 400;
}
#board-umi-lines.expanded {
  position: absolute;
  z-index: 10;
  inset: 0;
  width: 100%;
  max-width: none;
  min-height: 0;
  border-radius: 7px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .24);
}
#board-umi-lines.expanded #board-umi-lines-head::after { content: "タップで戻す"; }
#board-umi-lines-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
  padding: 5px;
  font-size: 13px;
}

/* ---------- 定石ツリー ---------- */
#tree-pane {
  flex: 1;
  min-width: 260px;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid #dde2e8;
  border-radius: 8px;
  overflow: hidden;
}
#tree-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #eef2f0;
  border-bottom: 1px solid #dde2e8;
  font-weight: 700;
  font-size: 14px;
}
#tree-head .spacer { flex: 1; }
#tree {
  flex: 1;
  overflow: auto;
  padding: 8px 8px 24px;
  font-size: 14px;
}
.tree-empty { color: #889; padding: 24px 12px; line-height: 1.8; }

.tkids { margin-left: 18px; border-left: 1px dotted #c4ccc8; padding-left: 4px; }
.trow {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 6px;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
}
.trow:hover { background: #eef4f1; }
.trow.current { background: #d9ecff; outline: 1px solid #9cc4ec; }

.twist {
  width: 16px;
  text-align: center;
  color: #567;
  cursor: pointer;
  flex-shrink: 0;
  font-size: 12px;
}
.twist.leaf { cursor: default; }

.tdisc {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}
.tdisc.black { background: #111; }
.tdisc.white { background: #fff; border: 1px solid #888; }

.tmove { font-family: Consolas, "Courier New", monospace; font-weight: 700; min-width: 26px; }
.tmove.root { font-family: inherit; font-weight: 700; color: #345; }

.tscore {
  font-size: 12px;
  font-weight: 700;
  border-radius: 9px;
  padding: 0 7px;
  min-width: 30px;
  text-align: center;
}
.tscore.pos { background: #dcf3e3; color: #157347; }
.tscore.zero { background: #e9ecef; color: #556; }
.tscore.neg { background: #fde3e0; color: #c0392b; }

.tjoseki {
  font-size: 11px;
  background: #efe7fb;
  color: #6b3fa0;
  border-radius: 9px;
  padding: 1px 8px;
}

/* ---------- 全画面定石マップ ---------- */
body.map-open { overflow: hidden; }
#map-pane {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
}
#map-pane[hidden] { display: none !important; }
#map-head {
  position: absolute;
  z-index: 12;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
  padding: 8px 14px;
  color: #fff;
  background: rgba(30, 58, 47, .94);
  backdrop-filter: blur(5px);
  flex-shrink: 0;
}
#map-head strong { font-size: 16px; }
#map-head .spacer { flex: 1; }
#btn-map-direction { padding-inline: 8px; font-size: 12px; white-space: nowrap; }
.map-launch {
  color: #fff;
  background: #2b6cb0;
  border-color: #245c98;
}
.map-launch:hover { background: #245c98; }

#map-wrap {
  flex: 1;
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}
#map-split {
  flex: 1;
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
}
#map-wrap.lines-open #map-split {
  grid-template-columns: minmax(0, 1fr);
}
#map-stage {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
#umi-lines-btn {
  position: absolute;
  z-index: 7;
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  min-height: 44px;
  padding: 8px 16px;
  color: #fff;
  background: #166b4a;
  border-color: #0f573c;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .22);
}
#umi-lines-btn:hover { background: #0f573c; }

/* 盤面・マップのどちらからでも開ける設定ポップアップ */
#umi-settings-backdrop {
  position: fixed;
  z-index: 220;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(12, 24, 19, .48);
}
#umi-settings-backdrop[hidden] { display: none !important; }
#umi-settings {
  width: min(420px, 100%);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 16px 44px rgba(0, 0, 0, .3);
}
#umi-settings-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid #dfe5e1;
  color: #fff;
  background: #1e3a2f;
}
#umi-settings-form {
  display: grid;
  gap: 12px;
  padding: 14px;
  color: #34453e;
  font-size: 13px;
}
#umi-settings-form label:not(.check-setting) {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
#umi-settings-form select {
  width: 112px;
  min-height: 44px;
  padding: 5px 8px;
  border: 1px solid #bfcac4;
  border-radius: 6px;
  background: #fff;
  font-size: 16px;
}
#umi-settings-form .check-setting {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
}
#umi-settings-form input[type="checkbox"] { width: 20px; height: 20px; }
#umi-total {
  padding-top: 10px;
  border-top: 1px solid #e5e9e7;
  color: #0a7a43;
  font-weight: 700;
  line-height: 1.45;
}
#btn-apply-umi-settings {
  min-height: 42px;
  color: #fff;
  background: #166b4a;
  border-color: #0f573c;
  font-weight: 700;
}
#btn-apply-umi-settings:hover { background: #0f573c; }

/* 読み込み・全book計算の進捗 */
#work-progress {
  position: fixed;
  z-index: 300;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(12, 24, 19, .45);
}
#work-progress-panel {
  width: min(360px, 100%);
  padding: 18px;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 16px 44px rgba(0, 0, 0, .3);
}
#work-progress-title { display: block; margin-bottom: 12px; color: #234437; }
#work-progress-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: #dce6e1;
}
#work-progress-fill {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: #16875b;
  transition: width .15s ease;
}
#work-progress.indeterminate #work-progress-fill {
  width: 42%;
  animation: work-progress-slide 1s ease-in-out infinite alternate;
}
#work-progress-detail {
  display: block;
  min-height: 1.4em;
  margin-top: 9px;
  color: #65736c;
  font-size: 12px;
}
@keyframes work-progress-slide {
  from { transform: translateX(-105%); }
  to { transform: translateX(245%); }
}

/* 覚える展開一覧 */
#umi-lines {
  position: absolute;
  z-index: 11;
  inset: 60px 8px 8px;
  border: 1px solid #ccd5d0;
  border-radius: 8px;
  max-height: none;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: #fff;
  box-shadow: 0 10px 32px rgba(0, 0, 0, .24);
}
#umi-lines[hidden] { display: none !important; }
#umi-lines-head {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 6px 10px;
  background: #f4f7f5;
  border-bottom: 1px solid #e6e9ec;
  font-size: 12px;
  font-weight: 700;
  color: #345;
}
#umi-lines-head .spacer { flex: 1; }
#umi-lines-body {
  overflow: auto;
  padding: 6px 10px;
  font-size: 13px;
}
.uline {
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr) auto auto;
  gap: 6px;
  padding: 7px 8px;
  border-radius: 5px;
  cursor: pointer;
  align-items: center;
  font-family: Consolas, "Courier New", monospace;
}
.uline:nth-child(even) { background: #f4f7f5; }
.uline:hover { background: #e8f2ed; }
.uline-num { color: #889; text-align: right; }
.uline-seq { color: #24342d; line-height: 1.55; overflow-wrap: anywhere; }
.uline-joseki {
  font-size: 11px;
  font-family: "Segoe UI", sans-serif;
  background: #efe7fb;
  color: #6b3fa0;
  border-radius: 8px;
  padding: 0 7px;
  margin-left: 0;
}
.uline-cut { color: #99a; }
#map-wrap.lines-open #map-legend { display: none; }

#map-canvas {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: block;
  touch-action: none;
  background: #fcfcfb;
}
#map-legend {
  position: absolute;
  left: 10px;
  bottom: 8px;
  font-size: 11px;
  color: #667;
  background: rgba(255,255,255,.85);
  border: 1px solid #e1e0d9;
  border-radius: 6px;
  padding: 4px 8px;
  line-height: 1.7;
  pointer-events: none;
}
.lg {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin: 0 3px 0 8px;
  vertical-align: -1px;
  border: 1px solid rgba(0,0,0,.15);
}
.lg-b { background: #2a78d6; }
.lg-n { background: #e7e6e2; }
.lg-w { background: #e34948; }

#map-tip {
  position: absolute;
  z-index: 5;
  background: rgba(20, 28, 24, .92);
  color: #fff;
  font-size: 12px;
  border-radius: 6px;
  padding: 6px 10px;
  pointer-events: none;
  line-height: 1.6;
  max-width: 320px;
}
#map-tip .tip-joseki {
  background: #efe7fb;
  color: #6b3fa0;
  border-radius: 8px;
  padding: 0 7px;
  font-size: 11px;
  font-weight: 700;
}

/* ---------- 狭い画面 ---------- */
@media (max-width: 960px) {
  :root { --cell: 44px; }
  body { overflow: hidden; height: 100dvh; }
  main { flex-direction: column; width: 100%; }
  #board-pane { flex: 1 1 auto; }
}

/* ---------- スマートフォン ---------- */
@media (max-width: 600px) {
  :root {
    /* 320px 幅でも、行ラベルと余白を含めて盤面全体が収まる。 */
    --cell: min(44px, calc((100vw - 40px) / 8));
  }

  #topbar {
    flex-wrap: wrap;
    gap: 8px;
    padding: max(8px, env(safe-area-inset-top)) 8px 8px;
  }
  #topbar h1 {
    flex: 1 1 auto;
    font-size: 17px;
  }
  #topbar h1 .sub { font-size: 11px; }
  #app-menu { order: initial; }
  #app-menu summary {
    min-height: 40px;
    padding: 6px 11px;
  }
  #app-menu-panel { width: min(240px, calc(100vw - 16px)); }
  main {
    gap: 12px;
    padding: 8px 8px max(12px, env(safe-area-inset-bottom));
  }
  #board-pane {
    width: 100%;
    align-items: center;
    min-height: 0;
    overflow: hidden;
  }
  #board-wrap { max-width: 100%; }
  #board { border-width: 2px; }
  #col-labels, #row-labels { font-size: 10px; }
  .score { font-size: clamp(12px, 4vw, 16px); }
  .score.best { font-size: clamp(14px, 4.5vw, 18px); }
  .legal-dot { width: 7px; height: 7px; }

  #game-status {
    width: 100%;
    justify-content: center;
    flex-wrap: wrap;
    gap: 4px 12px;
    font-size: 13px;
  }
  #controls {
    width: min(100%, calc(var(--cell) * 8 + 20px));
    gap: 6px;
  }
  .btn.nav {
    flex: 1;
    min-height: 44px;
    padding: 6px;
  }
  #kifu, #hint, #board-umi-lines {
    width: min(100%, calc(var(--cell) * 8 + 20px));
    max-width: none;
  }
  #board-pane.board-lines-open {
    --cell: max(29px, min(44px, calc((100vw - 40px) / 8), calc((100dvh - 390px) / 8)));
    gap: 6px;
  }
  #board-pane.board-lines-open #hint { display: none; }
  #board-pane.board-lines-open #board-umi-lines {
    width: 100%;
    min-height: 132px;
  }

  #map-head {
    min-height: 52px;
    padding: max(8px, env(safe-area-inset-top)) 8px 8px;
  }
  #map-head .btn { min-height: 38px; }

  #umi-total {
    width: 100%;
    line-height: 1.4;
  }
  #map-legend { display: none; }
  #map-tip { max-width: calc(100% - 16px); }

  #map-wrap.lines-open #map-split {
    grid-template-columns: minmax(0, 1fr);
  }
  #umi-lines {
    inset: max(60px, calc(env(safe-area-inset-top) + 52px)) 6px max(6px, env(safe-area-inset-bottom));
    max-height: none;
    min-height: 0;
  }
  #umi-lines-head {
    flex-wrap: wrap;
    padding: 8px;
  }
  #umi-lines-title {
    flex: 1 1 100%;
    line-height: 1.45;
  }
  #umi-lines-head .spacer { display: none; }
  #umi-lines-head .btn { min-height: 36px; }
  #umi-lines-body { padding: 6px 4px; }
  .uline {
    grid-template-columns: 26px minmax(0, 1fr) auto;
    gap: 5px;
    padding: 8px 6px;
  }
  .uline-cut { display: none; }
}

@media (max-width: 360px) {
  #topbar h1 .sub { display: block; margin: 2px 0 0; }
}
