/* ===== Variables ===== */
:root {
  --bg:      #1a1a2e;
  --surface: #16213e;
  --panel:   #0f3460;
  --accent:  #e94560;
  --accent2: #533483;
  --text:    #eaeaea;
  --muted:   #8888a8;
  --border:  #2a2a4a;
  --success: #4caf50;
  --radius:  8px;

  /* Layout dims (desktop) */
  --hdr:     40px;   /* header height */
  --pw:      188px;  /* panel width   */
  --tray-h:  136px;  /* tray area     */
  --gap:     8px;
  --pad:     8px;

  /*
   * Board fits inside the viewport leaving room for:
   *   vertical: header + tray + padding/gaps
   *   horizontal: panel + padding/gaps
   */
  --board-size: min(
    calc(100svh - var(--hdr) - var(--tray-h) - var(--pad) * 2 - var(--gap) * 3 - 4px),
    calc(100vw  - var(--pw)  - var(--pad) * 2 - var(--gap) * 2),
    450px
  );
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;           /* no page scroll */
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', 'Hiragino Kaku Gothic ProN', sans-serif;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

/* ===== App shell ===== */
.puzzle-app {
  height: 100svh;             /* full viewport, no scroll */
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: var(--pad) var(--pad) 0;
  overflow: hidden;
}

/* ===== Header ===== */
.puzzle-header {
  height: var(--hdr);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.puzzle-header h1 {
  font-size: clamp(1rem, 3.5vw, 1.5rem);
  color: var(--accent);
  white-space: nowrap;
}

.puzzle-desc {
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== Mobile bar (hidden on desktop) ===== */
.mobile-bar { display: none; }

/* ===== Main layout ===== */
.puzzle-layout {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: var(--gap);
  overflow: hidden;
}

/* ===== Left column ===== */
.puzzle-left {
  flex-shrink: 0;
  width: var(--board-size);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  overflow: hidden;
}

/* ===== Board ===== */
.puzzle-board {
  width: var(--board-size);
  height: var(--board-size);
  flex-shrink: 0;
  background: #0a0a1a;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.puzzle-board.active {
  display: grid;
  overflow: visible;
}

.board-placeholder {
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  padding: 1rem;
}

/* ===== Board Slots ===== */
.board-slot {
  background: #12122a;
  border: 1px solid rgba(255,255,255,0.05);
  border-radius: 2px;
  position: relative;
}

/* ドラッグオーバー */
.board-slot.drag-over {
  background: rgba(233,69,96,0.22);
  border: 1px dashed var(--accent);
}

/* タッチドラッグ用クローン */
.touch-clone {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.88;
  box-shadow: 0 10px 28px rgba(0,0,0,0.55);
  border: 2px solid var(--accent);
  border-radius: 3px;
  background-repeat: no-repeat;
}

/* トレイのドラッグオーバー */
.piece-tray.drag-over {
  border-color: var(--accent);
  border-style: dashed;
  background: rgba(233,69,96,0.07);
}

/* 空きマス：選択中のみクリック可を示す */
.board-slot.empty-slot {
  cursor: default;
}

.puzzle-board.has-selection .empty-slot {
  background: rgba(233,69,96,0.07);
  border: 1px dashed rgba(233,69,96,0.4);
  cursor: pointer;
}

.puzzle-board.has-selection .empty-slot:hover {
  background: rgba(233,69,96,0.18);
  border-color: var(--accent);
}

/* ===== Pieces ===== */
.puzzle-piece {
  display: block;
  background-repeat: no-repeat;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 2px;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  transition: box-shadow 0.12s, transform 0.12s, opacity 0.12s;
}

.puzzle-piece:hover {
  box-shadow: 0 0 0 2px var(--accent);
  position: relative;
  z-index: 2;
}

.puzzle-piece:active { cursor: grabbing; }

/* ドラッグ中（元の位置を薄く表示） */
.puzzle-piece.dragging {
  opacity: 0.2;
  cursor: grabbing;
}

/* 選択状態 */
.puzzle-piece.selected {
  box-shadow: 0 0 0 3px var(--accent), 0 0 14px rgba(233,69,96,0.45);
  transform: scale(1.06);
  position: relative;
  z-index: 5;
}

/* 正しい位置 */
.puzzle-piece.correct {
  box-shadow: inset 0 0 0 2px rgba(76,175,80,0.65);
}

.puzzle-piece.correct.selected {
  box-shadow: 0 0 0 3px var(--accent), inset 0 0 0 2px rgba(76,175,80,0.65);
}

/* 盤面フラッシュ（空きなし通知） */
@keyframes board-flash {
  0%, 100% { border-color: var(--border); }
  40%       { border-color: var(--accent); box-shadow: 0 0 12px rgba(233,69,96,0.4); }
}

.puzzle-board.flash {
  animation: board-flash 0.4s ease;
}

/* ===== Tray ===== */
.tray-section {
  height: var(--tray-h);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: hidden;
}

.tray-header {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}

.tray-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.tray-count {
  font-size: 0.72rem;
  color: var(--accent);
  font-weight: bold;
}

.piece-tray {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 3px;
  padding: 5px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  overflow-y: auto;
  overflow-x: hidden;
}

.piece-tray .puzzle-piece:hover {
  box-shadow: 0 0 0 2px var(--accent);
  transform: scale(1.08);
  position: relative;
  z-index: 2;
}

/* ===== Error ===== */
.error-message {
  background: rgba(233,69,96,0.1);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 0.6rem;
  text-align: center;
  flex-shrink: 0;
}

.error-message p { color: var(--accent); font-size: 0.82rem; margin-bottom: 0.4rem; }

/* ===== Panel ===== */
.puzzle-panel {
  width: var(--pw);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  padding-bottom: var(--pad);
}

/* Custom scrollbar */
.puzzle-panel::-webkit-scrollbar,
.piece-tray::-webkit-scrollbar { width: 4px; height: 4px; }
.puzzle-panel::-webkit-scrollbar-track,
.piece-tray::-webkit-scrollbar-track { background: transparent; }
.puzzle-panel::-webkit-scrollbar-thumb,
.piece-tray::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.panel-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  flex-shrink: 0;
}

.panel-section h2 {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

/* ===== Difficulty buttons ===== */
.difficulty-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
}

.btn-difficulty {
  padding: 0.35rem 0;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.82rem;
  transition: all 0.1s;
}

.btn-difficulty:hover { border-color: var(--accent); color: var(--accent); }

.btn-difficulty[aria-pressed="true"],
.btn-difficulty.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 700;
}

/* ===== Game info ===== */
.game-info { display: flex; flex-direction: column; gap: 0.25rem; }

.info-item { display: flex; justify-content: space-between; font-size: 0.82rem; }
.info-label { color: var(--muted); }
.info-value { font-weight: 700; font-variant-numeric: tabular-nums; }

/* ===== Best records ===== */
.best-records { font-size: 0.78rem; }

.best-record-item {
  display: flex;
  justify-content: space-between;
  padding: 0.18rem 0;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
}

.best-record-item:last-child { border-bottom: none; }
.best-record-val { color: var(--success); font-weight: 700; }
.no-record { color: var(--muted); font-size: 0.78rem; }

/* ===== Action buttons ===== */
.panel-actions { display: flex; flex-direction: column; gap: 0.35rem; }

.btn-primary, .btn-secondary {
  width: 100%;
  padding: 0.45rem 0.75rem;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  transition: all 0.1s;
  white-space: nowrap;
}

.btn-primary  { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled)  { background: #c73652; }

.btn-secondary { background: var(--panel); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--accent2); border-color: var(--accent2); }

.btn-primary:disabled,
.btn-secondary:disabled { opacity: 0.35; cursor: not-allowed; }

/* ===== Preview ===== */
.preview-area {
  width: 100%;
  aspect-ratio: 1;
  background: var(--panel);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 0.5rem;
}

.preview-img { width: 100%; height: 100%; object-fit: cover; }
.preview-placeholder { font-size: 0.68rem; color: var(--muted); text-align: center; padding: 0.4rem; }

.toggle-label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  cursor: pointer;
  color: var(--muted);
}

/* ===== Clear overlay ===== */
.clear-message {
  position: fixed;
  inset: 0;
  background: rgba(10,10,26,0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
}

.clear-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem 1.5rem;
  text-align: center;
  max-width: 300px;
  width: 90%;
}

.clear-content h2 { font-size: 1.8rem; color: var(--success); margin-bottom: 0.4rem; }

.clear-stats { color: var(--muted); font-size: 0.88rem; margin-bottom: 1rem; }

/* 画像個別メッセージ・URL */
.clear-image-info {
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  margin-bottom: 1.1rem;
  text-align: left;
}

.clear-image-message {
  color: var(--text);
  font-size: 0.875rem;
  line-height: 1.6;
  margin-bottom: 0;
}

.clear-image-message:has(+ .clear-image-url:not(.hidden)) {
  margin-bottom: 0.6rem;
}

.clear-image-url {
  display: inline-block;
  margin-top: 0.6rem;
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 600;
  text-decoration: none;
  border-bottom: 1px solid rgba(233,69,96,0.4);
  transition: border-color 0.1s, color 0.1s;
}

.clear-image-url:hover { color: #ff6b85; border-color: #ff6b85; }

.clear-actions { display: flex; flex-direction: column; gap: 0.4rem; }

/* ===== Utility ===== */
.hidden { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.noscript-msg { text-align: center; padding: 2rem; color: var(--accent); }

/* ===================================================
   MOBILE  (≤ 720px)
   - モバイルバーを表示
   - パネルを非表示
   - ボードは横幅いっぱい
   =================================================== */
@media (max-width: 720px) {
  :root {
    --hdr:    36px;
    --mob-bar: 78px;   /* mobile control bar height */
    --tray-h: 112px;
    --pw:     0px;

    --board-size: min(
      calc(100svh - var(--hdr) - var(--mob-bar) - var(--tray-h)
           - var(--pad) * 2 - var(--gap) * 4 - 4px),
      calc(100vw - var(--pad) * 2),
      450px
    );
  }

  /* モバイルバーを表示 */
  .mobile-bar {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex-shrink: 0;
    height: var(--mob-bar);
  }

  .mobile-diff {
    display: flex;
    gap: 4px;
  }

  .mobile-diff .btn-difficulty {
    flex: 1;
    padding: 0.3rem 0;
    font-size: 0.78rem;
  }

  .mobile-info {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
  }

  .mi-label { color: var(--muted); }
  .mi-label::after { content: ':'; }

  .mobile-info .info-value { font-size: 0.88rem; }
  /* spacer between moves and timer groups */
  .mobile-info span:nth-child(3) { margin-left: 12px; }

  .mobile-actions {
    display: flex;
    gap: 4px;
  }

  .mobile-actions .btn-primary,
  .mobile-actions .btn-secondary {
    flex: 1;
    padding: 0.3rem 0.25rem;
    font-size: 0.75rem;
  }

  /* デスクトップパネルを隠す */
  .puzzle-panel { display: none; }

  /* puzzle-leftを全幅に */
  .puzzle-left {
    width: 100%;
  }

  .puzzle-board {
    width: var(--board-size);
    height: var(--board-size);
    margin: 0 auto;
  }

  .tray-section {
    width: 100%;
  }
}

/* ===== Very small screens ===== */
@media (max-width: 360px) {
  :root {
    --pad: 4px;
    --gap: 5px;
  }
}
