:root {
  --ink: #2a2220;
  --paper: #fbf6ea;
  --green: #2f6e3a;
  --deep: #23485f;
  --gold: #d9a63a;
  --red: #c9443a;
  --line: rgba(42, 34, 32, 0.16);
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
  --sl: env(safe-area-inset-left, 0px);
  --sr: env(safe-area-inset-right, 0px);
  color-scheme: light;
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #cfe6f2;
  color: var(--ink);
  font-family: "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

#stage {
  position: fixed;
  inset: 0;
  touch-action: none;
}
#stage canvas { display: block; width: 100%; height: 100%; }

/* ---------- 画面（タイトル・選択・結果など） ---------- */
.screen {
  position: fixed;
  inset: 0;
  z-index: 20;
}
.sheet {
  display: grid;
  place-items: center;
  padding: calc(16px + var(--st)) 16px calc(16px + var(--sb));
  background: rgba(30, 44, 38, 0.38);
  backdrop-filter: blur(2px);
}
.panel {
  width: min(460px, 100%);
  max-height: 100%;
  background: var(--paper);
  border-radius: 18px;
  padding: 20px 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
  border: 1px solid var(--line);
}
.panel { overflow-y: auto; }
.panel h2 {
  margin: 0 0 14px;
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
  font-size: 22px;
  text-align: center;
  letter-spacing: 0.08em;
}

.menu-col { display: grid; gap: 10px; }
.menu-row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-top: 14px; }
.menu-row button { flex: 1 1 120px; }
.menu-col button, .menu-row button, .back {
  min-height: 48px;
  padding: 0 16px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  font-weight: 700;
}
.primary {
  background: var(--green) !important;
  border-color: var(--green) !important;
  color: #fff;
}
.back { width: 100%; margin-top: 14px; }

.title-screen {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  padding: calc(28px + var(--st)) 20px calc(24px + var(--sb));
  pointer-events: none;
}
.title-screen > * { pointer-events: auto; }
.logo { text-align: center; }
.logo h1 {
  margin: 0;
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
  font-weight: 900;
  font-size: clamp(40px, 11vw, 76px);
  letter-spacing: 0.06em;
  color: #fff;
  text-shadow: 0 3px 0 #23485f, 0 0 18px rgba(20, 50, 70, 0.55);
  -webkit-text-stroke: 1px #23485f;
}
.logo-sub {
  margin: 0 0 6px;
  font-size: clamp(15px, 4vw, 20px);
  font-weight: 700;
  letter-spacing: 0.3em;
  color: #fff;
  text-shadow: 0 2px 6px rgba(20, 40, 50, 0.7);
}
.title-bottom { display: grid; justify-items: center; width: 100%; }
.title-screen .menu-col { width: min(320px, 100%); }
.title-screen .menu-col button { background: rgba(255, 255, 255, 0.92); }
.gc-link {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  margin-top: 4px;
  padding: 0 12px;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.7);
}
.gc-menu-link {
  display: grid;
  place-items: center;
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  font-weight: 700;
  text-decoration: none;
}
.note {
  margin: 10px 0 0;
  font-size: 11px;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
}

/* 巻のカード */
.cards { display: grid; gap: 10px; }
.card {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 4px 10px;
  text-align: left;
  min-height: 64px;
  padding: 10px 14px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #fff;
}
.card b { font-family: "Hiragino Mincho ProN", "Yu Mincho", serif; font-size: 17px; }
.card small { grid-column: 1; font-size: 12px; opacity: 0.7; }
.card .card-stars { grid-row: 1 / span 2; grid-column: 2; font-size: 18px; color: var(--gold); letter-spacing: 2px; }
.card[disabled] { opacity: 0.45; cursor: default; }

#historyList h3 { margin: 14px 0 4px; font-size: 15px; color: var(--green); }
#historyList p { margin: 0; font-size: 14px; line-height: 1.7; }

/* ---------- 遊んでいるときの表示 ---------- */
.hud-top {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + var(--st)) calc(10px + var(--sr)) 8px calc(10px + var(--sl));
  background: linear-gradient(rgba(251, 246, 234, 0.95), rgba(251, 246, 234, 0.82));
  border-bottom: 1px solid var(--line);
}
.stage-name {
  font-family: "Hiragino Mincho ProN", "Yu Mincho", serif;
  font-weight: 800;
  font-size: 15px;
  white-space: nowrap;
}
.chips { display: flex; gap: 6px; flex: 1; flex-wrap: wrap; justify-content: center; }
.chip {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
  padding: 4px 9px;
  border-radius: 999px;
  background: #fff;
  border: 1px solid var(--line);
  font-size: 12px;
  white-space: nowrap;
}
.chip b { font-size: 15px; }
.chip small { font-size: 11px; opacity: 0.65; }
.chip.low b { color: var(--red); }
.chip.ok b { color: var(--green); }
.villages i {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #3f9a4f;
  border: 1px solid rgba(0, 0, 0, 0.2);
  align-self: center;
}
.villages i.warn { background: #f2b632; }
.villages i.flood { background: var(--red); }
.hud-buttons { display: flex; gap: 6px; }
.hud-buttons button {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: #fff;
  font-weight: 800;
  font-size: 17px;
}

.hud-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  gap: 8px;
  padding: 8px calc(10px + var(--sr)) calc(10px + var(--sb)) calc(10px + var(--sl));
  background: linear-gradient(rgba(251, 246, 234, 0.82), rgba(251, 246, 234, 0.96));
  border-top: 1px solid var(--line);
}
.tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tools button {
  display: grid;
  justify-items: center;
  align-content: center;
  min-height: 52px;
  padding: 4px 2px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  line-height: 1.1;
}
.tools button span { font-weight: 800; font-size: 15px; }
.tools button small { font-size: 10px; opacity: 0.65; }
.tools button[aria-pressed="true"] { background: var(--deep); border-color: var(--deep); color: #fff; }
.ico { display: block; width: 22px; height: 12px; margin-bottom: 3px; border-radius: 3px; }
.ico-levee { background: linear-gradient(#b0915c 0 50%, #8cc55c 50%); clip-path: polygon(20% 0, 80% 0, 100% 100%, 0 100%); }
.ico-dig { background: linear-gradient(90deg, #8cc55c 0 30%, #5aa7d8 30% 70%, #8cc55c 70%); }
.ico-undo { background: radial-gradient(circle at 50% 50%, transparent 35%, #888 36% 55%, transparent 56%); }
.ico-move { background: linear-gradient(90deg, transparent 45%, #888 45% 55%, transparent 55%), linear-gradient(transparent 40%, #888 40% 60%, transparent 60%); }

.flood-btn {
  min-height: 50px;
  border-radius: 14px;
  border: 0;
  background: linear-gradient(#3f7fb0, #2b5f8a);
  color: #fff;
  font-weight: 800;
  font-size: 17px;
  letter-spacing: 0.1em;
  box-shadow: 0 3px 0 #1d4566;
}
.flood-bar { display: flex; align-items: center; gap: 10px; min-height: 50px; }
.flood-label { font-weight: 800; white-space: nowrap; font-size: 14px; }
.bar { flex: 1; height: 12px; border-radius: 999px; background: rgba(42, 34, 32, 0.12); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #5aa7d8, #2b5f8a); }
#btnSpeed {
  min-width: 64px;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  font-weight: 800;
}

.toast {
  position: fixed;
  left: 50%;
  top: calc(64px + var(--st));
  transform: translateX(-50%);
  z-index: 15;
  max-width: min(92vw, 520px);
  padding: 8px 14px;
  border-radius: 12px;
  background: rgba(35, 72, 95, 0.92);
  color: #fff;
  font-size: 14px;
  line-height: 1.5;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  pointer-events: none;
}
.toast.ina::before { content: "伊奈忠次　"; font-weight: 800; color: #ffe08a; }

/* 伊奈忠次の言葉 */
.dialog {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 25;
  padding: 0 12px calc(14px + var(--sb));
  display: flex;
  justify-content: center;
}
.dialog-box {
  position: relative;
  width: min(640px, 100%);
  background: var(--paper);
  border: 2px solid #2f5d3a;
  border-radius: 16px;
  padding: 18px 16px 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
.who {
  position: absolute;
  top: -14px;
  left: 14px;
  padding: 2px 12px;
  border-radius: 999px;
  background: #2f5d3a;
  color: #fff;
  font-weight: 800;
  font-size: 13px;
}
.dialog-box p { margin: 4px 0 8px; font-size: 16px; line-height: 1.75; min-height: 3.5em; }
.dialog-box p.tip::before { content: "ヒント　"; font-weight: 800; color: var(--green); }
#btnDialog {
  display: block;
  margin-left: auto;
  min-height: 44px;
  padding: 0 18px;
  border-radius: 12px;
  border: 0;
  background: var(--green);
  color: #fff;
  font-weight: 800;
}

/* 結果 */
.result-stage { margin: 0; text-align: center; font-size: 13px; opacity: 0.7; }
.stars { text-align: center; font-size: 34px; letter-spacing: 6px; color: #d8d0bf; margin: -4px 0 8px; }
.stars .on { color: var(--gold); }
.result-rows { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.result-rows li {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 10px;
  background: #fff;
  border: 1px solid var(--line);
  font-size: 14px;
}
.result-rows .bad { color: var(--red); font-weight: 800; }
.result-rows .good { color: var(--green); font-weight: 800; }
.lesson { position: relative; margin-top: 18px; padding: 16px 12px 10px; border-radius: 12px; background: #fff; border: 1px solid var(--line); }
.lesson p { margin: 0; font-size: 14px; line-height: 1.7; }

/* 横長でも高さが低い画面（スマホ横）: 道具を右に縦並び */
@media (orientation: landscape) and (max-height: 520px) {
  .hud-top { padding-top: calc(4px + var(--st)); padding-bottom: 4px; }
  .stage-name { font-size: 13px; }
  .hud-bottom {
    left: auto;
    top: 52px;
    width: calc(150px + var(--sr));
    border-top: 0;
    border-left: 1px solid var(--line);
    align-content: end;
    background: rgba(251, 246, 234, 0.9);
  }
  .tools { grid-template-columns: repeat(2, 1fr); }
  .tools button { min-height: 46px; }
  .flood-btn { min-height: 48px; font-size: 15px; letter-spacing: 0; }
  .flood-bar { flex-wrap: wrap; gap: 6px; }
  .flood-bar .bar { flex-basis: 100%; order: 3; }
  .dialog-box p { min-height: 0; font-size: 15px; }
  .toast { top: calc(52px + var(--st)); }
  .logo h1 { font-size: 48px; }
  .title-screen { padding-top: calc(14px + var(--st)); }
  .title-screen .menu-col { grid-template-columns: 1fr 1fr 1fr; width: min(640px, 100%); }
  .sheet { padding-top: calc(10px + var(--st)); padding-bottom: calc(10px + var(--sb)); }
  .panel { width: min(760px, 100%); padding: 14px 16px; }
  .panel h2 { margin-bottom: 8px; font-size: 19px; }
  .cards { grid-template-columns: 1fr 1fr; }
  .back { margin-top: 10px; }
  #menu .menu-col { grid-template-columns: 1fr 1fr; }
  #result .panel { display: grid; grid-template-columns: 1fr 1fr; gap: 0 18px; align-items: center; }
  .stars { font-size: 28px; margin-bottom: 4px; }
  .result-rows li { padding: 6px 10px; }
  .lesson { margin-top: 12px; }
  .menu-row { margin-top: 10px; }
  .menu-row button { flex-basis: 30%; }
}

@media (min-width: 900px) and (min-height: 600px) {
  .hud-bottom { grid-template-columns: 1fr 260px; align-items: center; max-width: 900px; margin: 0 auto; border-radius: 16px 16px 0 0; border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
  .stage-name { font-size: 18px; }
}
