:root {
  color-scheme: light;
  --page: #f4f7f4;
  --ink: #1f2f35;
  --muted: #607077;
  --panel: #ffffff;
  --line: #d5ded9;
  --soft-line: #e8eee9;
  --green: #24786b;
  --green-dark: #15594f;
  --blue: #3f69b1;
  --orange: #d97938;
  --red: #bd4f4a;
  --yellow: #f4c84c;
  --shadow: 0 16px 34px rgb(31 47 53 / 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--page);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
}

button,
select {
  font: inherit;
}

button {
  touch-action: manipulation;
}

.app-shell {
  width: min(1240px, calc(100% - 28px));
  margin: 0 auto;
  padding: 22px 0 32px;
}

.top-bar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 14px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--green-dark);
  font-size: 0.92rem;
  font-weight: 800;
}

h1 {
  margin: 0;
  font-size: clamp(1.9rem, 3.4vw, 3rem);
  line-height: 1.12;
  letter-spacing: 0;
}

.session-board {
  display: grid;
  grid-template-columns: repeat(3, minmax(82px, 1fr));
  gap: 8px;
  min-width: min(370px, 100%);
}

.session-board div {
  min-height: 72px;
  padding: 9px 10px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 8px 18px rgb(31 47 53 / 0.07);
  text-align: center;
}

.session-board span {
  display: block;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.session-board strong {
  display: block;
  color: var(--green-dark);
  font-size: clamp(1.35rem, 2.4vw, 1.85rem);
  line-height: 1.25;
}

.settings-panel,
.board-panel,
.pieces-panel {
  border: 2px solid var(--line);
  border-radius: 8px;
  background: rgb(255 255 255 / 0.96);
  box-shadow: var(--shadow);
}

.settings-panel {
  display: grid;
  grid-template-columns: 1.15fr 0.8fr 0.8fr 0.95fr 0.95fr 0.9fr;
  gap: 12px;
  align-items: end;
  margin-bottom: 14px;
  padding: 14px;
}

.setting-group {
  min-width: 0;
}

.setting-group.wide {
  min-width: 220px;
}

.setting-label {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 900;
}

.segmented {
  display: grid;
  grid-auto-columns: minmax(0, 1fr);
  grid-auto-flow: column;
  gap: 6px;
}

.segment,
.select-control,
.action-button,
.hint-button {
  min-height: 48px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  font-weight: 850;
}

.segment {
  min-width: 0;
  min-height: 58px;
  padding: 6px 8px;
  font-size: clamp(0.82rem, 1.1vw, 0.98rem);
  line-height: 1.3;
  overflow-wrap: anywhere !important;
  white-space: normal !important;
  word-break: normal !important;
}

.segment:hover,
.segment:focus-visible,
.select-control:focus-visible,
.action-button:hover,
.action-button:focus-visible,
.hint-button:hover,
.hint-button:focus-visible {
  outline: 3px solid rgb(63 105 177 / 0.22);
  border-color: var(--blue);
}

.segment.is-active {
  border-color: var(--green);
  background: #e8f4ef;
  color: var(--green-dark);
  box-shadow: inset 0 -5px 0 rgb(36 120 107 / 0.14);
}

.select-control {
  width: 100%;
  padding: 0 12px;
}

.setting-actions {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: minmax(180px, 1.1fr) minmax(170px, 1fr) minmax(170px, 1fr);
  gap: 8px;
}

.action-button {
  padding: 0 16px;
}

.action-button.primary {
  border-color: var(--green-dark);
  background: var(--green);
  color: #fff;
}

.action-button.danger {
  border-color: #e5b8b5;
  background: #fff3f2;
  color: #933b37;
}

.practice-layout {
  display: grid;
  grid-template-columns: minmax(500px, 1fr) minmax(320px, 0.45fr);
  gap: 14px;
  align-items: stretch;
}

.board-panel,
.pieces-panel {
  min-height: 640px;
  padding: 16px;
}

.board-header,
.pieces-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.mode-summary,
.status-text,
.pieces-header p,
.sample-box p {
  margin: 0;
}

.mode-summary {
  color: var(--green-dark);
  font-size: 0.95rem;
  font-weight: 900;
}

.status-text,
.pieces-header p,
.sample-box p,
#traySummary {
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 700;
}

.hint-button {
  min-width: 122px;
  background: #fff7dd;
  border-color: #ecd27d;
  color: #775612;
}

.board-stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(120px, 160px);
  gap: 12px;
  align-items: start;
}

.puzzle-board {
  --grid-size: 2;
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--grid-size), minmax(0, 1fr));
  width: min(100%, 68vh, 720px);
  aspect-ratio: 1;
  margin: 0 auto;
  border: 3px solid #96aaa2;
  border-radius: 8px;
  overflow: hidden;
  background: #f9fbf8;
}

.slot {
  position: relative;
  border-right: 1px solid var(--soft-line);
  border-bottom: 1px solid var(--soft-line);
  background-color: #fbfdfb;
}

.slot::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: var(--sample-image);
  background-position: var(--sample-position);
  background-size: calc(var(--grid-size) * 100%) calc(var(--grid-size) * 100%);
  opacity: 0;
  transition: opacity 180ms ease;
}

.puzzle-board.show-sample .slot::before,
.puzzle-board.flash-sample .slot::before {
  opacity: 0.22;
}

.puzzle-board.flash-sample .slot::before {
  opacity: 0.5;
}

.slot.is-target {
  box-shadow: inset 0 0 0 4px rgb(244 200 76 / 0.9);
  animation: targetPulse 900ms ease-in-out infinite;
}

.slot.is-placed {
  background: #fff;
}

.placed-piece {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: default;
}

.placed-piece canvas,
.piece canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.sample-box {
  padding: 10px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fbfdfb;
}

.sample-box img {
  display: block;
  width: 100%;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.sample-box.is-hidden img {
  filter: blur(8px);
  opacity: 0.18;
}

.pieces-panel {
  display: flex;
  flex-direction: column;
}

.pieces-header h2 {
  margin: 0 0 4px;
  font-size: 1.25rem;
  line-height: 1.25;
  letter-spacing: 0;
}

#pieceCountLabel {
  flex: 0 0 auto;
  min-width: 78px;
  padding: 8px 10px;
  border-radius: 8px;
  background: #eaf3f0;
  color: var(--green-dark);
  text-align: center;
  font-weight: 900;
}

.piece-bank {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(74px, 1fr));
  gap: 10px;
  align-content: start;
  min-height: 0;
  max-height: 560px;
  padding: 4px 2px 12px;
  overflow: auto;
}

.piece {
  position: relative;
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-height: 64px;
  border: 2px solid #b9c7c0;
  border-radius: 8px;
  background: #ffffff;
  cursor: grab;
  touch-action: none;
  user-select: none;
  box-shadow: 0 8px 18px rgb(31 47 53 / 0.1);
}

.piece:focus-visible {
  outline: 4px solid rgb(63 105 177 / 0.35);
}

.piece.is-active {
  border-color: var(--orange);
  box-shadow: 0 0 0 4px rgb(217 121 56 / 0.18), 0 8px 18px rgb(31 47 53 / 0.1);
}

.piece.is-dragging {
  position: fixed;
  z-index: 30;
  width: var(--drag-size);
  height: var(--drag-size);
  pointer-events: none;
  cursor: grabbing;
  box-shadow: 0 16px 38px rgb(31 47 53 / 0.22);
}

.piece.is-locked {
  cursor: default;
}

.piece::after {
  content: attr(data-rotate-label);
  position: absolute;
  right: 5px;
  bottom: 5px;
  min-width: 26px;
  min-height: 22px;
  padding: 2px 5px;
  border-radius: 6px;
  background: rgb(31 47 53 / 0.78);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 900;
  line-height: 1.3;
  opacity: var(--rotation-badge, 0);
}

.piece.is-placed-effect {
  animation: popIn 260ms ease-out;
}

.celebration {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
  place-items: center;
  overflow: hidden;
  background: rgb(31 47 53 / 0.2);
}

.celebration.is-visible {
  display: grid;
}

.celebration-card {
  width: min(360px, calc(100% - 40px));
  padding: 26px 22px;
  border: 3px solid #f4c84c;
  border-radius: 8px;
  background: #fffdf7;
  text-align: center;
  box-shadow: 0 24px 54px rgb(31 47 53 / 0.24);
}

.celebration-card h2 {
  margin: 0 0 8px;
  font-size: 2.2rem;
  letter-spacing: 0;
}

.celebration-card p {
  margin: 0 0 18px;
  color: var(--muted);
  font-weight: 800;
}

.celebration-mark {
  margin: 0 0 6px;
  color: var(--yellow);
  font-size: 3rem;
}

.spark {
  position: fixed;
  z-index: 55;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--spark-color);
  pointer-events: none;
  animation: sparkFly 920ms ease-out forwards;
}

@keyframes targetPulse {
  50% {
    box-shadow: inset 0 0 0 6px rgb(244 200 76 / 0.55);
  }
}

@keyframes popIn {
  0% {
    transform: scale(0.9);
  }
  70% {
    transform: scale(1.04);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes sparkFly {
  0% {
    opacity: 1;
    transform: translate(0, 0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(var(--spark-x), var(--spark-y)) scale(0.4);
  }
}

@media (max-width: 1040px) {
  .settings-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .practice-layout {
    grid-template-columns: 1fr;
  }

  .board-panel,
  .pieces-panel {
    min-height: 0;
  }

  .piece-bank {
    grid-template-columns: repeat(auto-fill, minmax(70px, 1fr));
    max-height: 280px;
  }
}

@media (max-width: 740px) {
  .app-shell {
    width: min(100% - 20px, 1240px);
    padding-top: 12px;
  }

  .top-bar,
  .board-header,
  .pieces-header {
    align-items: stretch;
    flex-direction: column;
  }

  .session-board,
  .settings-panel,
  .setting-actions,
  .board-stage {
    grid-template-columns: 1fr;
  }

  .segmented {
    grid-auto-flow: row;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #sizeControls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .puzzle-board {
    width: min(100%, 86vw);
  }

  .sample-box {
    max-width: 180px;
  }

  .hint-button,
  .action-button {
    width: 100%;
  }
}

/* LiNest v3: パズル図案・ヒント色・ドラッグ寸法は変更しない */
body.linest-themed {
  --page: var(--linest-bg);
  --ink: var(--linest-text);
  --muted: var(--linest-muted);
  --panel: var(--linest-surface);
  --line: var(--linest-border);
  --soft-line: #f4ede2;
  --green: var(--linest-green);
  --green-dark: var(--linest-green-dark);
  --orange: var(--linest-terracotta);
  --shadow: var(--linest-shadow);
}

body.linest-themed .app-shell,
body.linest-themed .top-bar > *,
body.linest-themed .settings-panel > *,
body.linest-themed .practice-layout > *,
body.linest-themed .board-header > *,
body.linest-themed .pieces-header > * {
  min-width: 0;
}

body.linest-themed .top-bar {
  padding: 18px 20px;
  border: 1px solid var(--linest-border);
  border-left: 6px solid var(--linest-green-bright);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--linest-shadow);
}

body.linest-themed .settings-panel,
body.linest-themed .board-panel,
body.linest-themed .pieces-panel,
body.linest-themed .session-board div {
  border-color: var(--linest-border);
  background: rgba(255, 255, 255, 0.97);
  box-shadow: var(--linest-shadow);
}

body.linest-themed .settings-panel {
  background: #f9fcf6;
}

body.linest-themed :where(.segment, .select-control, .action-button, .hint-button) {
  min-width: 0;
  border-color: var(--linest-border);
  line-height: 1.35;
  white-space: normal;
}

body.linest-themed .segment.is-active {
  border-color: var(--linest-green);
  color: var(--linest-green-dark);
  background: var(--linest-green-soft);
  box-shadow: inset 0 -5px 0 rgba(47, 158, 68, 0.14);
}

body.linest-themed .piece,
body.linest-themed .sample-box {
  border-color: var(--linest-border);
  box-shadow: 0 7px 16px rgba(74, 55, 40, 0.08);
}

@media (max-width: 740px) {
  body.linest-themed .top-bar {
    padding: 16px;
  }

  body.linest-themed :where(.segment, .select-control, .action-button, .hint-button) {
    padding-inline: 7px;
    font-size: 0.94rem;
  }
}
