:root {
  --bg: #f2f5f8;
  --panel: #ffffff;
  --line: #d7dee6;
  --text: #1d2b36;
  --muted: #5b6b7a;
  --sea: #dbeefb;
  --land: #eef1f4;
  --land-line: #b8c4cf;
  --accent: #1f7a8c;
  --accent-dark: #14505c;
  --ok: #2e9e5b;
  --ng: #d9534f;
  --shadow: 0 2px 8px rgba(20, 40, 60, .12);
}

* { box-sizing: border-box; }

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

/* GAS の iframe のように、親の高さが決まらない場所でも画面がつぶれないようにする */
body {
  min-height: 100vh;
}

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", "Meiryo", system-ui, sans-serif;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

/* ---------------------------------------------------------- ヘッダー */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
  z-index: 5;
}

.title {
  margin: 0;
  font-size: 18px;
  letter-spacing: .04em;
  white-space: nowrap;
}

.stats {
  display: flex;
  gap: 12px;
  font-size: 13px;
  color: var(--muted);
}

.stat {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  padding: 2px 10px;
  background: #f4f7fa;
  border: 1px solid var(--line);
  border-radius: 999px;
}

.stat b {
  font-size: 16px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  margin-left: auto;
}

.ctl {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.ctl select {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.btn {
  font: inherit;
  font-size: 13px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover { background: #f0f5f8; }
.btn:active { transform: translateY(1px); }

.btn-toggle.is-on {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-size: 15px;
  padding: 10px 22px;
}

/* ---------------------------------------------------------- レイアウト */
.layout {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.map-area {
  position: relative;
  flex: 1 1 auto;
  min-height: 240px;   /* 高さが取れないときでも地図が見えるようにする */
  background: var(--sea);
  overflow: hidden;
  touch-action: none;
}

.map {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

.map.is-panning { cursor: grabbing; }

/* 地図の各レイヤー */
.guide-path {
  fill: var(--land);
  stroke: var(--land-line);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

.guide-path.is-target {
  fill: #ffe9a8;
  stroke: #e0a800;
  stroke-width: 2.5;
  animation: hintPulse 1s ease-in-out 3;
}

@keyframes hintPulse {
  0%, 100% { opacity: .35; }
  50%      { opacity: 1; }
}

/* 出題範囲の外（地形モードでは日本全体）は「すでにある陸地」として表示する */
.guide-path.is-context {
  fill: #e6eaee;
  stroke: #c3ccd4;
}

.no-guide .guide-path,
.no-guide .guide-path.is-context {
  fill: var(--land);
  stroke: none;
}

/* ---- 地形（山地・山脈／河川／平野／半島／工業地帯）のしるし ---- */
.mark {
  opacity: .42;
  transition: opacity .2s;
}

.mark-line {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.mark.is-done { opacity: .95; }

.mark.is-target {
  opacity: 1;
  animation: hintPulse 1s ease-in-out 3;
}

.no-guide .mark { opacity: .22; }   /* むずかしいモードでは、しるしも薄く */

.mark.just-placed { animation: pop .45s ease-out; }

.map-label-feature { font-weight: 700; }

.placed-path {
  stroke: #fff;
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

.placed-path.just-placed { animation: pop .45s ease-out; }

@keyframes pop {
  0%   { opacity: .2; filter: brightness(2); }
  40%  { opacity: 1;  filter: brightness(1.6); }
  100% { opacity: 1;  filter: brightness(1); }
}

.map-label {
  font-family: inherit;
  fill: #17323f;
  paint-order: stroke;
  stroke: rgba(255, 255, 255, .9);
  stroke-width: 3px;
  stroke-linejoin: round;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
}

.hide-labels .map-label { display: none; }

/* 地方の色分けの凡例（「色分けつき」のときだけ出る） */
.legend {
  position: absolute;
  left: 10px;
  top: 10px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 7px 10px;
  font-size: 12px;
  color: var(--text);
  background: rgba(255, 255, 255, .9);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  pointer-events: none;
}

.legend[hidden] { display: none; }

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.legend-item i {
  width: 13px;
  height: 13px;
  border-radius: 3px;
  display: inline-block;
}

/* 地図まわりのUI */
.map-tools {
  position: absolute;
  right: 10px;
  top: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.zbtn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  height: 38px;
  padding: 0 8px;
  font: inherit;
  font-size: 14px;
  line-height: 1;
  white-space: nowrap;          /* 「全体」が2行に折れてはみ出すのを防ぐ */
  background: rgba(255, 255, 255, .94);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.zbtn:active { background: #e9f1f5; }

.map-help {
  position: absolute;
  left: 10px;
  bottom: 8px;
  margin: 0;
  padding: 5px 10px;
  font-size: 12px;
  color: var(--muted);
  background: rgba(255, 255, 255, .82);
  border-radius: 6px;
  pointer-events: none;
}

.toast {
  position: absolute;
  left: 50%;
  top: 14px;
  transform: translate(-50%, -12px);
  padding: 8px 18px;
  border-radius: 999px;
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  background: var(--ok);
  box-shadow: var(--shadow);
  opacity: 0;
  transition: opacity .18s, transform .18s;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast.ng { background: var(--ng); }

/* ---------------------------------------------------------- ピース置き場 */
.tray {
  flex: 0 0 auto;
  background: var(--panel);
  border-top: 1px solid var(--line);
  box-shadow: 0 -2px 8px rgba(20, 40, 60, .08);
}

.tray-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 5px 14px 0;
  font-size: 12px;
  color: var(--muted);
}

.tray-title { font-weight: 700; }

.tray-selected {
  flex: 1 1 auto;
  color: var(--accent-dark);
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tray-list {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  height: 132px;
  padding: 6px 14px 12px;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
}

.piece {
  flex: 0 0 auto;
  position: relative;
  width: 96px;
  height: 112px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 2px;
  padding: 4px;
  background: #fbfdff;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: pan-x;
  transition: box-shadow .12s, border-color .12s, transform .12s;
}

.piece:hover { border-color: var(--accent); box-shadow: var(--shadow); }

.piece.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(31, 122, 140, .35);
}

.piece.is-dragging { opacity: .3; }

.piece.shake { animation: shake .4s; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.piece svg {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  pointer-events: none;
}

.piece-name {
  font-size: 12px;
  line-height: 1.2;
  color: var(--text);
  pointer-events: none;
  white-space: nowrap;
}

.hide-piece-name .piece-name { visibility: hidden; }

/* 地形モードのピース（名前の札） */
.piece-label {
  width: auto;
  height: 60px;
  align-self: center;
  justify-content: center;
  gap: 1px;
  padding: 6px 14px;
  border-width: 2px;
}

.label-name {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  pointer-events: none;
}

.label-kana {
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
  pointer-events: none;
}

.hide-piece-kana .label-kana { display: none; }

/* ドラッグ中に指／カーソルに追従するピース */
.ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 50;
  pointer-events: none;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, .35));
  will-change: transform;
}

.ghost path {
  stroke: #fff;
  stroke-width: 1.5;
}

.ghost-label {
  padding: 6px 14px;
  background: #fff;
  border: 2px solid var(--accent);
  border-radius: 10px;
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
}


/* ---------------------------------------------------------- クリア画面 */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 25, 35, .55);
  z-index: 100;
}

.overlay[hidden] { display: none; }

.clear-card {
  background: #fff;
  border-radius: 16px;
  padding: 24px 32px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .3);
  max-width: min(92vw, 420px);
}

.clear-emoji { font-size: 44px; margin: 0; }
.clear-card h2 { margin: 4px 0 14px; font-size: 22px; }

.clear-stats {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin: 0 0 12px;
}

.clear-stats div { text-align: center; }
.clear-stats dt { font-size: 12px; color: var(--muted); }
.clear-stats dd {
  margin: 2px 0 0;
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.clear-rank {
  margin: 0 0 16px;
  font-size: 15px;
  color: var(--accent-dark);
  font-weight: 700;
}

/* ---------------------------------------------------------- 画面が狭いとき */
@media (max-width: 760px) {
  .topbar { gap: 6px 10px; padding: 6px 10px; }
  .title { font-size: 15px; }
  .controls { width: 100%; margin-left: 0; }
  .ctl select { font-size: 12px; }
  .tray-list { height: 118px; }
  .piece { width: 84px; height: 100px; }
  .map-help { font-size: 11px; max-width: 60%; }
}
