/* 解けるソリティア */

:root {
  --felt: #1e6b3c;
  --bar: #144a2a;
  --bar-fg: #f4f8f5;
  --slot: rgba(255, 255, 255, 0.38);
  --slot-fill: rgba(0, 0, 0, 0.08);
  --dlg: #ffffff;
  --dlg-fg: #1c1f1d;
  --muted: #5d6661;
  --line: #cfd6d2;
  --accent: #1e6b3c;
  --accent-weak: #e3f1e8;
  --danger: #b3261e;
  --hint: #ffd54a;
  --target: #8fd8ff;
  --toast: #1c1f1d;
  --toast-fg: #ffffff;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --felt: #0f3a22;
  --bar: #0a2516;
  --bar-fg: #e3ebe6;
  --slot: rgba(255, 255, 255, 0.28);
  --slot-fill: rgba(0, 0, 0, 0.12);
  --dlg: #1f2522;
  --dlg-fg: #e6ebe8;
  --muted: #a3ada7;
  --line: #3a453f;
  --accent: #3f9d63;
  --accent-weak: #22392b;
  --danger: #f2827a;
  --toast: #e6ebe8;
  --toast-fg: #111;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --felt: #0f3a22;
    --bar: #0a2516;
    --bar-fg: #e3ebe6;
    --slot: rgba(255, 255, 255, 0.28);
    --slot-fill: rgba(0, 0, 0, 0.12);
    --dlg: #1f2522;
    --dlg-fg: #e6ebe8;
    --muted: #a3ada7;
    --line: #3a453f;
    --accent: #3f9d63;
    --accent-weak: #22392b;
    --danger: #f2827a;
    --toast: #e6ebe8;
    --toast-fg: #111;
    color-scheme: dark;
  }
}

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

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

body {
  background: var(--felt);
  color: var(--bar-fg);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", Meiryo, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

.noscript {
  margin: 0;
  padding: 3em 1em;
  text-align: center;
  font-size: 18px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

button {
  font: inherit;
  color: inherit;
}

/* ---- 画面の骨組み ---- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  background: var(--felt);
}

.toolbar {
  flex: none;
  background: var(--bar);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
  user-select: none;
  -webkit-user-select: none;
}

.tools {
  display: flex;
  justify-content: center;
  gap: 2px;
  padding: 4px 4px 0;
}

.tool {
  appearance: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  min-width: 58px;
  padding: 5px 6px 3px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-size: 11px;
  line-height: 1.3;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
}

.tool svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tool:hover { background: rgba(255, 255, 255, 0.12); }
.tool:focus-visible { outline: 2px solid var(--hint); outline-offset: -2px; }
.tool:disabled { opacity: 0.4; cursor: default; background: transparent; }

.menu-wrap { position: relative; }

.menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 9500;
  min-width: 200px;
  padding: 6px;
  border-radius: 10px;
  background: var(--dlg);
  color: var(--dlg-fg);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
}

.menu button {
  display: block;
  width: 100%;
  padding: 10px 14px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.menu button:hover, .menu button:focus-visible { background: var(--accent-weak); outline: none; }

.status {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  column-gap: 14px;
  padding: 1px 8px 5px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.status b { font-weight: 700; }
.status .deal { opacity: 0.85; }
.status .unverified {
  margin-left: 6px;
  padding: 0 6px;
  border-radius: 4px;
  background: rgba(255, 213, 74, 0.2);
  font-size: 12px;
}

@media (min-width: 720px) {
  .tools { gap: 6px; padding-top: 6px; }
  .tool { flex-direction: row; gap: 6px; padding: 6px 12px; font-size: 14px; }
  .status { font-size: 14px; column-gap: 22px; }
}

/* 横向きのスマホなど高さが足りないときは1行にまとめる */
@media (max-height: 500px) and (orientation: landscape) {
  .toolbar { display: flex; align-items: center; justify-content: space-between; padding-bottom: 2px; }
  .tools { padding: 2px; }
  .tool { flex-direction: row; gap: 4px; padding: 4px 8px; font-size: 12px; min-width: 0; }
  .tool svg { width: 18px; height: 18px; }
  .status { padding: 0 10px; font-size: 12px; column-gap: 10px; }
}

@media (max-width: 380px) and (orientation: portrait) {
  .tool { min-width: 0; padding-left: 4px; padding-right: 4px; letter-spacing: -0.02em; }
}

/* ---- 盤面 ---- */

.board {
  position: relative;
  flex: 1;
  min-height: 0;
  margin: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.card, .slot {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--cw, 70px);
  height: var(--ch, 98px);
}

.card {
  will-change: transform;
  cursor: pointer;
}

.card .inner {
  position: absolute;
  inset: 0;
  border-radius: calc(var(--cw, 70px) * 0.07);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.card .face, .card .back {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background-size: 100% 100%;
  background-repeat: no-repeat;
}

.card .back { background-image: var(--back); }
.card .face { display: none; }
.card.up .face { display: block; }
.card.up .back { display: none; }

.card.dragging { cursor: grabbing; }
.card.dragging .inner { box-shadow: 0 8px 18px rgba(0, 0, 0, 0.45); }

.slot {
  display: grid;
  place-items: center;
  border: 2px solid var(--slot);
  border-radius: calc(var(--cw, 70px) * 0.07);
  background: var(--slot-fill);
  color: var(--slot);
  font-size: calc(var(--cw, 70px) * 0.42);
  font-weight: 700;
  line-height: 1;
}

.slot-w { border-style: dashed; border-color: rgba(255, 255, 255, 0.18); background: transparent; }
.slot-s { cursor: pointer; }
.slot-s[data-mark="recycle"]::after { content: "↻"; font-size: 1.2em; }
.slot-s[data-mark="none"]::after { content: "×"; font-size: 1.3em; }

.card.hint .inner, .slot.hint, .card.hint-to .inner, .slot.hint-to {
  box-shadow: 0 0 0 3px var(--hint), 0 0 12px var(--hint);
}

.card.target .inner, .slot.target {
  box-shadow: 0 0 0 3px var(--target);
}

.win-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 8000;
}

body.loading .board::after {
  content: "配っています…";
  position: absolute;
  inset: 0;
  z-index: 7000;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.15);
  font-size: 16px;
}

/* ---- 自動で完成・通知 ---- */

.autocomplete {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 6000;
  transform: translateX(-50%);
  padding: 10px 26px;
  border: 0;
  border-radius: 999px;
  background: var(--hint);
  color: #1c1f1d;
  font-weight: 700;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  cursor: pointer;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 9000;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 12px;
  width: max-content;
  max-width: calc(100vw - 24px);
  padding: 10px 14px;
  transform: translateX(-50%);
  border-radius: 12px;
  background: var(--toast);
  color: var(--toast-fg);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  font-size: 14px;
}

.toast-actions { display: flex; gap: 6px; }
.toast-actions button {
  padding: 5px 12px;
  border: 1px solid currentColor;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
}
.toast-actions button.close { border-color: transparent; padding: 5px 8px; }

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

dialog {
  width: min(92vw, 460px);
  max-height: min(88vh, 88dvh);
  padding: 20px 20px 16px;
  border: 0;
  border-radius: 14px;
  background: var(--dlg);
  color: var(--dlg-fg);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
  overflow: auto;
  overscroll-behavior: contain;
}

dialog.wide { width: min(94vw, 680px); }
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }

dialog h2 { margin: 0 0 12px; font-size: 20px; }
dialog form { position: relative; }

.x {
  float: right;
  width: 36px;
  height: 36px;
  margin: -8px -8px 0 8px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}
.x:hover { background: var(--accent-weak); }
dialog h3 { margin: 18px 0 6px; font-size: 16px; }
dialog p { margin: 0 0 10px; }

fieldset {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 14px;
  padding: 0;
  border: 0;
}

legend {
  float: left;
  width: 100%;
  margin-bottom: 4px;
  padding: 0;
  font-size: 14px;
  font-weight: 700;
}

fieldset label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px 5px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

fieldset label:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-weak);
}

fieldset input { margin: 0; accent-color: var(--accent); }

.backs label { padding: 6px 10px 6px 8px; }
.back-sample {
  display: inline-block;
  width: 34px;
  height: 48px;
  background-size: 100% 100%;
}

.note { color: var(--accent); font-size: 14px; font-weight: 700; }
.note.small { color: var(--muted); font-size: 13px; font-weight: 400; }

.buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}
.buttons.start { justify-content: flex-start; margin-top: 0; }

.buttons button, .field input {
  min-height: 40px;
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
}

.buttons button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.buttons button.danger { border-color: var(--danger); color: var(--danger); }
button:focus-visible { outline: 3px solid var(--target); outline-offset: 2px; }

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 14px;
  font-size: 14px;
  font-weight: 700;
}

.field input {
  width: 100%;
  cursor: text;
  background: var(--dlg);
  color: var(--dlg-fg);
  font-size: 18px;
  font-weight: 400;
  font-variant-numeric: tabular-nums;
}

.error { color: var(--danger); font-size: 14px; }
hr { margin: 18px 0 12px; border: 0; border-top: 1px solid var(--line); }

.result {
  display: flex;
  justify-content: center;
  gap: 28px;
  margin: 8px 0 12px;
  text-align: center;
}
.result dt { color: var(--muted); font-size: 13px; }
.result dd { margin: 0; font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.bonus { margin-top: -6px; color: var(--muted); font-size: 13px; text-align: center; }
.records { color: var(--accent); font-weight: 700; text-align: center; }
#dlg-win h2 { text-align: center; font-size: 24px; }

table.stats {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  font-size: 14px;
}
table.stats th, table.stats td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
table.stats th { text-align: left; font-weight: 400; color: var(--muted); }
table.stats thead th { color: var(--dlg-fg); font-weight: 700; text-align: right; }
table.stats td { text-align: right; font-weight: 700; }

.help ul { margin: 0 0 10px; padding-left: 1.3em; }
.help li { margin-bottom: 4px; }

.fig-layout {
  display: grid;
  gap: 6px;
  margin: 8px 0 4px;
  padding: 10px;
  border-radius: 10px;
  background: var(--felt);
  color: #fff;
  font-size: 11px;
  text-align: center;
}
.fl-row { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.fl-box, .fl-col { border: 1.5px solid rgba(255, 255, 255, 0.6); border-radius: 5px; padding: 10px 0; }
.fl-col { padding-bottom: 34px; }

.fig-row { display: flex; flex-wrap: wrap; gap: 20px; margin: 8px 0; }
figure { margin: 0; }
figcaption { margin-top: 4px; color: var(--muted); font-size: 13px; }
.fig-cards { display: flex; gap: 6px; min-height: 80px; padding: 10px; border-radius: 10px; background: var(--felt); }
.fig-cards img { width: 56px; height: 78px; }
.fig-cards.fan { flex-direction: column; gap: 0; width: max-content; }
.fig-cards.fan img + img { margin-top: -58px; }
