/* 神経衰弱固有スタイル */

.mem-settings {
  margin-top: 4px;
}

.mem-field .field-label {
  margin-bottom: 8px;
}

.option-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.option-tab {
  appearance: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 48px;
  padding: 8px 6px;
  border: 1px solid var(--md-sys-color-outline-variant);
  border-radius: var(--md-sys-shape-corner-medium);
  background: var(--md-sys-color-surface);
  color: var(--md-sys-color-on-surface);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.04s ease;
}

.option-tab small {
  font-size: 0.7rem;
  font-weight: 500;
  opacity: 0.75;
}

.option-tab:hover {
  background: var(--md-sys-color-primary-container);
}

.option-tab:active {
  transform: translateY(1px);
}

.option-tab.is-active {
  background: var(--md-sys-color-primary);
  border-color: var(--md-sys-color-primary);
  color: var(--md-sys-color-on-primary);
}

.game-panel {
  margin-top: 20px;
}

.mem-board-wrap {
  display: flex;
  justify-content: center;
  width: 100%;
  max-width: 100%;
  margin-bottom: 10px;
  overflow: hidden;
}

.mem-board {
  --mem-cols: 4;
  --mem-gap: clamp(5px, 1.5vw, 12px);
  --mem-radius: clamp(6px, 1.4vw, 12px);
  --mem-aspect: 1.35;
  --mem-pad: clamp(8px, 2vw, 16px);
  --mem-card-max: 100px;

  box-sizing: border-box;
  display: grid;
  grid-template-columns: repeat(var(--mem-cols), minmax(0, 1fr));
  gap: var(--mem-gap);
  width: 100%;
  max-width: min(
    100%,
    calc(
      (var(--mem-cols) * var(--mem-card-max))
      + ((var(--mem-cols) - 1) * var(--mem-gap))
      + (2 * var(--mem-pad))
    )
  );
  margin: 0 auto;
  padding: var(--mem-pad);
  border: 1px solid rgba(96, 154, 214, 0.35);
  border-radius: var(--md-sys-shape-corner-large);
  background:
    radial-gradient(ellipse 80% 50% at 10% 0%, rgba(63, 196, 255, 0.14), transparent 55%),
    radial-gradient(ellipse 70% 45% at 90% 100%, rgba(122, 157, 255, 0.12), transparent 50%),
    linear-gradient(160deg, #16264a 0%, #101c38 55%, #0d1730 100%);
  box-shadow: 0 14px 34px rgba(28, 52, 96, 0.24), inset 0 0 0 1px rgba(255, 255, 255, 0.04);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.mem-board[data-difficulty="hard"] {
  --mem-gap: clamp(4px, 1.1vw, 8px);
  --mem-aspect: 1.3;
  --mem-card-max: 78px;
  --mem-pad: clamp(6px, 1.6vw, 12px);
}

.mem-board[data-difficulty="easy"] {
  --mem-card-max: 108px;
}

.mem-board[data-difficulty="normal"] {
  --mem-card-max: 96px;
}

.mem-card {
  position: relative;
  display: block;
  width: 100%;
  min-width: 0;
  aspect-ratio: 1 / var(--mem-aspect);
  height: auto;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  font-family: Outfit, "Noto Sans JP", sans-serif;
  touch-action: manipulation;
  perspective: 900px;
  container-type: inline-size;
}

.mem-card:focus-visible {
  outline: 2px solid #3fc4ff;
  outline-offset: 3px;
  border-radius: var(--mem-radius);
}

.mem-card.is-flipped .mem-card__inner,
.mem-card.is-matched .mem-card__inner {
  transform: rotateY(180deg);
}

.mem-card.is-selected:not(.is-matched) .mem-card__face {
  box-shadow:
    0 0 0 2px rgba(63, 196, 255, 0.75),
    0 0 14px rgba(63, 196, 255, 0.45),
    0 2px 6px rgba(8, 16, 36, 0.28);
}

.mem-card.is-matched {
  cursor: default;
  pointer-events: none;
}

.mem-card.is-matched .mem-card__face {
  filter: brightness(0.88);
  box-shadow:
    0 0 0 2px rgba(80, 220, 170, 0.65),
    0 0 16px rgba(80, 220, 170, 0.4),
    0 2px 6px rgba(8, 16, 36, 0.28);
  animation: mem-match-glow 420ms ease-out;
}

@keyframes mem-match-glow {
  0% {
    transform: rotateY(180deg) scale(1);
    box-shadow:
      0 0 0 2px rgba(80, 220, 170, 0.9),
      0 0 22px rgba(80, 220, 170, 0.65),
      0 2px 6px rgba(8, 16, 36, 0.28);
  }
  40% {
    transform: rotateY(180deg) scale(1.04);
  }
  100% {
    transform: rotateY(180deg) scale(1);
    box-shadow:
      0 0 0 2px rgba(80, 220, 170, 0.65),
      0 0 16px rgba(80, 220, 170, 0.4),
      0 2px 6px rgba(8, 16, 36, 0.28);
  }
}

.mem-card.is-locked {
  pointer-events: none;
}

.mem-card__inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 280ms ease;
}

.mem-card__face,
.mem-card__back {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  box-sizing: border-box;
  border-radius: var(--mem-radius);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: 0 2px 6px rgba(8, 16, 36, 0.3);
}

.mem-card__back {
  border: 1px solid rgba(90, 170, 255, 0.45);
  background:
    linear-gradient(135deg, rgba(63, 196, 255, 0.18), transparent 42%),
    linear-gradient(315deg, rgba(122, 157, 255, 0.2), transparent 40%),
    repeating-linear-gradient(
      135deg,
      #152448 0 6px,
      #101c38 6px 12px
    );
  box-shadow:
    inset 0 0 0 2px rgba(63, 196, 255, 0.22),
    0 2px 6px rgba(8, 16, 36, 0.35);
}

.mem-card__back::after {
  content: "";
  position: absolute;
  inset: 16%;
  border: 1.5px solid rgba(120, 210, 255, 0.55);
  border-radius: calc(var(--mem-radius) - 2px);
  background:
    radial-gradient(circle at 50% 50%, rgba(63, 196, 255, 0.35), transparent 62%),
    linear-gradient(160deg, transparent 40%, rgba(122, 157, 255, 0.25) 100%);
  pointer-events: none;
}

.mem-card__face {
  transform: rotateY(180deg);
  border: 1px solid rgba(20, 36, 68, 0.14);
  background: linear-gradient(180deg, #ffffff 0%, #f3f7fd 100%);
  color: var(--mem-rank-color, #1a2438);
}

.mem-card__rank {
  font-size: clamp(1.1rem, 48cqw, 2.75rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.02em;
}

.mem-guide {
  margin: 0 0 12px;
  color: var(--md-sys-color-on-surface-variant);
  font-size: 0.8125rem;
  line-height: 1.55;
  text-align: center;
}

.game-controls {
  justify-content: center;
}

@media (max-width: 520px) {
  .option-tabs {
    gap: 8px;
  }

  .option-tab {
    min-height: 44px;
    padding: 7px 4px;
    font-size: 0.8125rem;
  }

  .option-tab small {
    font-size: 0.65rem;
  }

  .mem-board {
    --mem-gap: 5px;
    --mem-pad: 8px;
  }

  .mem-board[data-difficulty="hard"] {
    --mem-gap: 4px;
    --mem-pad: 6px;
  }

  .mem-card__rank {
    font-size: clamp(1rem, 46cqw, 2.2rem);
  }
}

/* 共通プレイ画面の配置はパイプパズルに準拠。 */
html.gs-playing .game-panel {
  display: grid;
  grid-template-columns: 44px minmax(0, 506px);
  grid-template-rows: auto auto auto auto;
  grid-template-areas:
    "bar settings"
    "status status"
    "board board"
    "controls controls";
  column-gap: 10px;
  justify-content: center;
  align-content: start;
}

html.gs-playing .game-panel > .gs-bar {
  grid-area: bar;
}

html.gs-playing .mem-settings {
  grid-area: settings;
  justify-self: stretch;
  width: 100%;
  max-width: none;
  margin: 0 0 10px;
}

html.gs-playing .mem-settings .field-label {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html.gs-playing .option-tabs {
  gap: 6px;
}

html.gs-playing .option-tab {
  flex-direction: column;
  gap: 0;
  min-height: 44px;
  padding: 4px 6px;
  border-color: var(--play-border);
  background: var(--play-hud-bg);
  color: var(--play-text);
  font-size: .8125rem;
}

html.gs-playing .option-tab small {
  font-size: .6875rem;
}

@media (hover: hover) {
  html.gs-playing .option-tab:hover {
    border-color: color-mix(in srgb, var(--play-accent) 55%, var(--play-border));
    background: var(--play-btn-hover);
  }
}

html.gs-playing .option-tab.is-active {
  border-color: var(--play-accent);
  background: color-mix(in srgb, var(--play-accent) 18%, var(--play-hud-bg));
  color: var(--play-accent);
}

html.gs-playing .option-tab:focus-visible {
  outline: 2px solid var(--play-accent);
  outline-offset: 2px;
}

html.gs-playing .mem-board-wrap {
  grid-area: board;
  justify-self: center;
  width: 100%;
  max-width: none;
  margin: 0 0 10px;
  container-type: inline-size;
}

html.gs-playing .mem-stage {
  width: 100%;
}

html.gs-playing .game-panel .game-controls {
  grid-area: controls;
  justify-content: center;
  gap: 12px;
  margin: 0;
}

/* PCは最大640pxを基準にし、盤面は高さにも収める。 */
@media (min-width: 768px) and (min-height: 600px) {
  html.gs-playing .game-panel {
    grid-template-columns: 44px minmax(0, 586px);
    padding-top: max(clamp(24px, 5vh, 56px), env(safe-area-inset-top));
  }

  html.gs-playing .game-panel > .gs-bar,
  html.gs-playing .mem-settings {
    margin-bottom: 28px;
  }

  html.gs-playing .gs-hud,
  html.gs-playing .mem-board-wrap,
  html.gs-playing .game-panel .game-controls {
    max-width: 640px;
  }

  html.gs-playing .gs-hud,
  html.gs-playing .mem-board-wrap {
    margin-bottom: 24px;
  }
}

/* スマートフォン横向きなど高さが足りない画面では、戻る・操作・難易度・情報を左、盤面を右に並べる */
/* 操作ボタンは戻るボタンと同じ行の右端に置き、左列の高さを抑える */
@media (orientation: landscape) and (max-height: 500px) {
  html.gs-playing .game-panel {
    grid-template-columns: minmax(200px, 280px) minmax(0, 1fr);
    grid-template-rows: auto auto auto 1fr;
    grid-template-areas:
      "bar board"
      "settings board"
      "status board"
      ". board";
    column-gap: 16px;
  }

  html.gs-playing .game-panel > .gs-bar {
    justify-self: start;
  }

  html.gs-playing .mem-settings {
    justify-self: stretch;
    width: 100%;
  }

  html.gs-playing .game-panel .game-controls {
    grid-area: bar;
    justify-self: end;
    width: auto;
    margin-bottom: 10px;
  }

  html.gs-playing .mem-board-wrap {
    align-items: center;
    min-height: 0;
    margin: 0;
    container-type: size;
  }

  html.gs-playing .mem-board {
    --mem-available: calc(100dvh - 30px);
  }
}

.mem-result {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 20px 16px;
  border-radius: var(--md-sys-shape-corner-large);
  background: rgba(11, 20, 42, 0.92);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  text-align: center;
  animation: mem-result-in 180ms ease-out;
}

.mem-result__title {
  margin: 0;
  color: #a8f0ff;
  font-family: "Outfit", "Noto Sans JP", sans-serif;
  font-size: clamp(1.5rem, 7vw, 2.1rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  text-shadow: 0 0 18px rgba(96, 220, 255, 0.7), 0 0 34px rgba(150, 106, 255, 0.45);
}

.mem-result__detail {
  margin: 0;
  color: rgba(230, 240, 255, 0.86);
  font-size: 0.9375rem;
  line-height: 1.6;
}

.mem-result__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 12px;
}

.mem-result__actions .btn {
  min-width: 8.5rem;
  justify-content: center;
}

@keyframes mem-result-in {
  from { opacity: 0; }
  to { opacity: 1; }
}


.mem-stage { position: relative; width: 100%; }
html.gs-playing .game-panel { --mem-available: calc(100dvh - 270px); }
html.gs-playing .gs-hud { grid-area: status; max-width: 640px; }
html.gs-playing .mem-board {
  /* カードの縦横比・列数を保ち、盤面全体を残りの高さに収める。 */
  --mem-card-max: max(20px, min(96px, (var(--mem-available) - 2 * var(--mem-pad) - (var(--mem-rows) - 1) * var(--mem-gap)) / var(--mem-rows) / var(--mem-aspect)));
  border-color: var(--play-border);
}
html.gs-playing .mem-board[data-difficulty="easy"] { --mem-rows: 3; }
html.gs-playing .mem-board[data-difficulty="normal"],
html.gs-playing .mem-board[data-difficulty="hard"] { --mem-rows: 4; }
@media (min-width: 768px) and (min-height: 600px) {
  html.gs-playing .game-panel { --mem-available: calc(100dvh - 300px); }
}
@media (orientation: landscape) and (max-height: 500px) {
  html.gs-playing .game-panel { --mem-available: calc(100dvh - 30px); }
}
@media (prefers-reduced-motion: reduce) {
  .mem-card__inner { transition: none; }
  .mem-result, .mem-card.is-matched .mem-card__face { animation: none; }
}

@media (max-width: 360px) {
  html.gs-playing .option-tab { padding-inline: 2px; font-size: .75rem; }
  html.gs-playing .option-tab small { font-size: .6rem; }
}
