:root {
  color-scheme: light;
  --bg: #e7ecf1;
  --panel: #ffffff;
  --border: #c6cfd9;
  --text: #1f2933;
  --muted: #5d6975;
  --accent: #2c67a8;
  --accent-soft: #e6eef8;
  --danger: #c62828;
  --danger-soft: #fdecea;
  --success: #2e7d32;
  --success-soft: #e7f4e8;
  --hover: #e4ecf5;
  --disabled: #9aa4ae;
  --board-border: #5b6e85;
  font-family: "Segoe UI", "Yu Gothic UI", "Meiryo", "Hiragino Sans", "Noto Sans JP", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
}

body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
}

button {
  font: inherit;
  color: inherit;
}

kbd {
  font: inherit;
}

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

/* ---- メニュー ---- */

.menubar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  gap: 2px;
  padding: 3px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  user-select: none;
  -webkit-user-select: none;
}

.menu {
  position: relative;
}

.menu-title {
  padding: 5px 12px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  cursor: default;
}

.menu-title:hover,
.menu.open .menu-title {
  background: var(--hover);
}

.menu-items {
  position: absolute;
  top: calc(100% + 3px);
  left: 0;
  min-width: 200px;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 8px 24px rgba(20, 35, 55, 0.18);
}

.menu-items button {
  position: relative;
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  padding: 6px 10px 6px 30px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  text-align: left;
  white-space: nowrap;
  cursor: default;
}

.menu-items button:hover:not(:disabled),
.menu-items button:focus-visible {
  background: var(--hover);
  outline: none;
}

.menu-items button:disabled {
  color: var(--disabled);
}

.menu-items button[aria-checked="true"]::before {
  content: "✓";
  position: absolute;
  left: 10px;
  font-weight: 700;
}

.menu-items kbd {
  margin-left: auto;
  color: var(--muted);
  font-size: 12px;
}

.menu-items button:disabled kbd {
  color: var(--disabled);
}

.menu-items .sep {
  height: 1px;
  margin: 4px 6px;
  background: var(--border);
}

/* ---- ゲーム画面 ---- */

.app {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
}

.window {
  min-width: 0;
  max-width: 100%;
  padding: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(20, 35, 55, 0.08);
}

.statusbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin-bottom: 6px;
}

.state {
  min-width: 7.5em;
  padding: 4px 10px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  text-align: center;
}

.state[data-state="lost"] {
  background: var(--danger-soft);
  color: var(--danger);
}

.state[data-state="won"] {
  background: var(--success-soft);
  color: var(--success);
}

.counter {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-width: 3.5em;
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.counter svg {
  width: 18px;
  height: 18px;
  flex: none;
}

.spacer {
  flex: 1 1 0;
}

.btn {
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #f5f7fa;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover:not(:disabled) {
  background: #e9eef3;
}

.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn:disabled {
  color: var(--disabled);
  cursor: default;
}

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

.btn.primary:hover:not(:disabled) {
  background: #245a94;
}

.btn.danger {
  border-color: #e3a5a5;
  color: var(--danger);
}

.message {
  min-height: 1.5em;
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 13px;
}

.message[data-tone="lost"] {
  color: var(--danger);
  font-weight: 600;
}

.message[data-tone="won"] {
  color: var(--success);
  font-size: 15px;
  font-weight: 700;
}

.board-wrap {
  max-width: 100%;
  overflow: auto;
}

#board {
  display: block;
  box-sizing: content-box; /* 枠線の分で縮んでにじまないように */
  margin: 0 auto;
  border: 1px solid var(--board-border);
  cursor: default;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.noscript {
  padding: 12px 16px;
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 6px;
}

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

dialog {
  width: min(420px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 16px 48px rgba(20, 35, 55, 0.25);
}

dialog.wide {
  width: min(680px, calc(100vw - 32px));
}

dialog::backdrop {
  background: rgba(20, 30, 45, 0.35);
}

.dialog-body {
  padding: 18px 20px 16px;
}

.dialog-body h2 {
  margin: 0 0 14px;
  font-size: 17px;
}

.dialog-body h3 {
  margin: 18px 0 6px;
  font-size: 15px;
}

.dialog-body ul {
  margin: 0;
  padding-left: 1.3em;
}

.dialog-body li + li {
  margin-top: 4px;
}

.fields {
  display: grid;
  grid-template-columns: auto 7em auto;
  align-items: center;
  gap: 10px 12px;
}

.fields input {
  width: 100%;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  font-variant-numeric: tabular-nums;
}

.fields input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
}

.range,
.hint {
  color: var(--muted);
  font-size: 12px;
}

.hint {
  margin: 12px 0 0;
}

.buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

.best-table,
.help-table {
  width: 100%;
  border-collapse: collapse;
}

.best-table th,
.best-table td,
.help-table th,
.help-table td {
  padding: 7px 8px;
  border-bottom: 1px solid #e3e8ee;
  text-align: left;
  vertical-align: top;
}

.best-table th {
  font-weight: 600;
}

.best-table .time {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  text-align: right;
  white-space: nowrap;
}

.best-table .date {
  color: var(--muted);
  white-space: nowrap;
  text-align: right;
}

.help-table th {
  width: 42%;
  font-weight: 600;
  white-space: normal;
}

@media (max-width: 480px) {
  .app {
    padding: 8px;
  }

  .window {
    padding: 8px;
  }

  .help-table th,
  .help-table td {
    display: block;
    width: auto;
    border-bottom: 0;
    padding: 2px 4px;
  }

  .help-table tr {
    display: block;
    padding: 6px 0;
    border-bottom: 1px solid #e3e8ee;
  }
}
