/* ── CSS custom properties ─────────────────────────────────────────── */
:root {
  --tile-sz:  128px;
  --tile-gap: 12px;
}

/* ── Reset & base ──────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: #eef2f7;
  color: #2d3748;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── Header ────────────────────────────────────────────────────────── */
#app-header {
  background: #1a2744;
  color: #fff;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}

#app-header h1 {
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

#btn-back {
  display: inline-flex;
  align-items: center;
  padding: 5px 13px;
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 7px;
  color: #fff;
  font-size: 0.84rem;
  font-weight: 600;
  text-decoration: none;
  transition: background 0.15s;
  flex-shrink: 0;
}
#btn-back:hover { background: rgba(255,255,255,0.28); }

/* Timer */
#timer-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

#timer-display {
  font-family: 'Courier New', monospace;
  font-size: 1.55rem;
  font-weight: 700;
  background: rgba(0,0,0,0.35);
  color: #fff;
  padding: 3px 12px;
  border-radius: 6px;
  min-width: 76px;
  text-align: center;
  letter-spacing: 0.05em;
}

.btn-timer {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background 0.15s;
  min-width: 70px;
  text-align: center;
}
.btn-timer:hover { background: rgba(255,255,255,0.28); }

/* ── Settings bar ──────────────────────────────────────────────────── */
#settings-bar {
  background: #fff;
  border-bottom: 2px solid #dde3ed;
  padding: 8px 20px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  flex-shrink: 0;
}

.setting-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.setting-group > label:first-child {
  font-size: 0.82rem;
  font-weight: 600;
  color: #4a5568;
  white-space: nowrap;
}

.setting-group select,
.setting-group input[type="number"] {
  padding: 5px 8px;
  border: 1px solid #cbd5e0;
  border-radius: 6px;
  font-size: 0.88rem;
  background: #f8fafc;
  color: #2d3748;
  outline-color: #4299e1;
}

.setting-group input[type="number"] { width: 58px; }

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.82rem;
  font-weight: 600;
  color: #4a5568;
  cursor: pointer;
  white-space: nowrap;
}
.checkbox-label input[type="checkbox"] { cursor: pointer; accent-color: #3182ce; }

.btn-primary {
  margin-left: auto;
  background: #3182ce;
  color: #fff;
  border: none;
  padding: 8px 24px;
  border-radius: 8px;
  font-size: 0.98rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.btn-primary:hover  { background: #2b6cb0; }
.btn-primary:active { transform: scale(0.97); }

/* ── Target picker ─────────────────────────────────────────────────── */
.btn-targets {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border: 1px solid #cbd5e0;
  border-radius: 6px;
  background: #f8fafc;
  color: #2d3748;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-targets:hover       { background: #edf2f7; }
.btn-targets.is-open     { background: #e6f0fb; border-color: #4299e1; }
.btn-targets.is-filtered { border-color: #3182ce; color: #2b6cb0; }
.btn-targets .targets-count {
  font-weight: 500;
  color: #4a5568;
  background: #edf2f7;
  padding: 1px 6px;
  border-radius: 10px;
  font-size: 0.75rem;
}
.btn-targets.is-filtered .targets-count {
  background: #bee3f8;
  color: #2b6cb0;
}

#target-panel {
  background: #fafbfd;
  border-bottom: 2px solid #dde3ed;
  padding: 10px 20px;
  max-height: 40vh;
  overflow-y: auto;
}
.target-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.target-panel-title {
  font-weight: 700;
  color: #2d3748;
  font-size: 0.9rem;
}
.target-panel-actions {
  display: flex;
  gap: 6px;
}
.target-panel-actions button {
  padding: 4px 10px;
  border: 1px solid #cbd5e0;
  background: #fff;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  color: #2d3748;
  cursor: pointer;
}
.target-panel-actions button:hover { background: #edf2f7; }
.btn-targets-close {
  border: none !important;
  background: transparent !important;
  font-size: 1.2rem !important;
  line-height: 1;
  padding: 2px 8px !important;
}
.target-panel-body {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 4px 12px;
}
.target-panel-empty {
  padding: 12px;
  color: #718096;
  font-style: italic;
}
.target-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 4px;
  font-size: 0.82rem;
  color: #2d3748;
  cursor: pointer;
  border-radius: 4px;
}
.target-row:hover { background: #edf2f7; }
.target-row input[type="checkbox"] { cursor: pointer; accent-color: #3182ce; }
.target-thumb {
  width: 32px;
  height: 32px;
  object-fit: contain;
  flex-shrink: 0;
  border-radius: 4px;
  border: 1px solid #e2e8f0;
  background: #f7fafc;
}
.target-row-label { flex: 1 1 auto; }

/* ── Extra settings panel ──────────────────────────────────────────── */
.btn-extra {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border: 1px solid #cbd5e0;
  border-radius: 6px;
  background: #f8fafc;
  color: #2d3748;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-extra:hover   { background: #edf2f7; }
.btn-extra.is-open { background: #e6f0fb; border-color: #4299e1; }

#extra-panel {
  background: #fafbfd;
  border-bottom: 2px solid #dde3ed;
  padding: 10px 20px;
  max-height: 40vh;
  overflow-y: auto;
}
.extra-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.extra-panel-title {
  font-weight: 700;
  color: #2d3748;
  font-size: 0.9rem;
}
.btn-extra-close {
  border: none !important;
  background: transparent !important;
  font-size: 1.2rem;
  line-height: 1;
  padding: 2px 8px;
  cursor: pointer;
}
.extra-panel-body {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px 20px;
  align-items: center;
}
.extra-panel-body .checkbox-label { padding: 4px 0; }
.extra-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── Game area ─────────────────────────────────────────────────────── */
#game-area {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 28px;
  padding: 24px 28px;
  align-items: flex-start;
  justify-content: center;
}

.area-label {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #718096;
  margin-bottom: 8px;
  text-align: center;
}

/* Sample card */
#sample-section { display: flex; flex-direction: column; align-items: center; }

#sample-card {
  width: 190px;
  height: 190px;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.13);
  border: 2px solid #e2e8f0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#sample-img {
  max-width: 88%;
  max-height: 88%;
  object-fit: contain;
  display: block;
}

/* Comparison grid */
#comp-section { display: flex; flex-direction: column; align-items: center; position: relative; }

#comp-grid {
  display: grid;
  gap: var(--tile-gap);
  grid-template-columns: repeat(var(--grid-cols, 4), var(--tile-sz));
  justify-content: center;
}

/* ── Tile ──────────────────────────────────────────────────────────── */
.tile-wrapper {
  /* Expand animation lives here so it doesn't conflict with 3D flip */
  transition: transform 0.25s ease;
}
.tile-wrapper.expanding {
  transform: scale(1.12);
}

.tile {
  width: var(--tile-sz);
  height: var(--tile-sz);
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.55s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
}

.tile.flipped {
  transform: rotateY(180deg);
}

.tile-face {
  position: absolute;
  inset: 0;
  border-radius: 12px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border: 2px solid #e2e8f0;
  background: #fff;
  box-shadow: 0 3px 8px rgba(0,0,0,0.10);
  overflow: hidden;
}

/* Front face */
.tile-front {
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.tile-front img {
  max-width: 95%;
  max-height: 95%;
  object-fit: contain;
  pointer-events: none;
  display: block;
}
.tile:not(.tile-disabled):hover .tile-front {
  border-color: #4299e1;
  box-shadow: 0 4px 12px rgba(66,153,225,0.28);
}
.tile.tile-disabled { cursor: default; }

/* Back face — neutral default; green only when outcome is Correct */
.tile-back {
  transform: rotateY(180deg);
  background: linear-gradient(135deg, #4a5568, #718096);
  display: flex;
  align-items: center;
  justify-content: center;
}
.tile-back.back-correct {
  background: linear-gradient(135deg, #276749, #38a169);
}
.ok-text {
  font-size: 2.6rem;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 2px 6px rgba(0,0,0,0.25);
  letter-spacing: 0.03em;
}

/* ── Tile animations ───────────────────────────────────────────────── */

/* Jiggle on wrong click */
@keyframes jiggle {
  0%   { transform: translateX(0)        rotate(0deg); }
  15%  { transform: translateX(-8px)     rotate(-3deg); }
  30%  { transform: translateX(8px)      rotate(3deg); }
  45%  { transform: translateX(-6px)     rotate(-2deg); }
  60%  { transform: translateX(6px)      rotate(2deg); }
  75%  { transform: translateX(-3px)     rotate(-1deg); }
  90%  { transform: translateX(3px)      rotate(1deg); }
  100% { transform: translateX(0)        rotate(0deg); }
}
.tile-wrapper.jiggle { animation: jiggle 0.5s ease; }

/* Red flash on wrong click */
@keyframes flash-red {
  0%   { background: #fff; }
  20%  { background: #fed7d7; }
  50%  { background: #fc8181; }
  100% { background: #fff; }
}
.tile-wrapper.flash-red .tile-front { animation: flash-red 0.5s ease; }

/* ── Prompt effects ────────────────────────────────────────────────── */

/* Sparkle */
@keyframes sparkle-pulse {
  0%   { box-shadow: 0 0 6px 3px #ffd700,
                     0 0 14px 6px rgba(255,215,0,0.55),
                     0 0 24px 10px rgba(255,165,0,0.25); }
  50%  { box-shadow: 0 0 12px 6px #ffd700,
                     0 0 28px 12px rgba(255,215,0,0.65),
                     0 0 44px 20px rgba(255,140,0,0.35); }
  100% { box-shadow: 0 0 6px 3px #ffd700,
                     0 0 14px 6px rgba(255,215,0,0.55),
                     0 0 24px 10px rgba(255,165,0,0.25); }
}
.tile.prompt-sparkle .tile-front {
  animation: sparkle-pulse 1s ease-in-out infinite;
  border-color: #d69e2e !important;
}

/* Outline */
.tile.prompt-outline .tile-front {
  border: 3px solid #38a169 !important;
  box-shadow: 0 0 0 3px rgba(56,161,105,0.38), 0 3px 8px rgba(0,0,0,0.1) !important;
  background: rgba(56,161,105,0.05);
}

/* ── Trial overlay: Next + Retry watermark buttons ─────────────────── */
#trial-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  pointer-events: none;
  z-index: 20;
}

.btn-watermark {
  pointer-events: all;
  padding: 11px 30px;
  border-radius: 12px;
  font-size: 1.1rem;
  font-weight: 800;
  cursor: pointer;
  border: none;
  box-shadow: 0 4px 18px rgba(0,0,0,0.28);
  transition: opacity 0.15s, transform 0.1s;
  letter-spacing: 0.02em;
}
.btn-watermark:hover  { opacity: 0.93; transform: scale(1.04); }
.btn-watermark:active { transform: scale(0.97); }

.btn-watermark-next {
  background: rgba(47, 133, 90, 0.92);
  color: #fff;
}
.btn-watermark-retry {
  background: rgba(192, 86, 33, 0.88);
  color: #fff;
}

/* ── Bottom bar ────────────────────────────────────────────────────── */
#bottom-bar {
  background: #fff;
  border-top: 2px solid #dde3ed;
  padding: 9px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.btn-prompt {
  background: #dd6b20;
  color: #fff;
  border: none;
  padding: 5px 14px;
  border-radius: 7px;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-prompt:hover { background: #c05621; }

.btn-print {
  background: #4a5568;
  color: #fff;
  border: none;
  padding: 7px 18px;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-print:hover { background: #2d3748; }

.btn-clear-data {
  background: #c53030;
  color: #fff;
  border: none;
  padding: 7px 18px;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-clear-data:hover { background: #9b2c2c; }

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 34px;
  padding: 0;
}
.btn-icon svg { width: 20px; height: 20px; fill: currentColor; }

/* ── Print section (screen: hidden; print: visible) ─────────────────── */
#print-section { display: none; }

@media print {
  /* Hide everything except the print section */
  body > *:not(#print-section) { display: none !important; }

  #print-section {
    display: block;
    padding: 18px 24px;
    font-family: Arial, sans-serif;
    font-size: 11pt;
    color: #000;
  }

  #print-section h2 {
    font-size: 15pt;
    margin-bottom: 4px;
  }

  #print-meta {
    font-size: 9pt;
    color: #444;
    margin-bottom: 14px;
  }

  #results-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 14px;
  }

  #results-table th,
  #results-table td {
    border: 1px solid #666;
    padding: 5px 8px;
    text-align: left;
    vertical-align: top;
  }

  #results-table th {
    background: #e8e8e8;
    font-weight: bold;
    font-size: 9.5pt;
  }

  #results-table tr:nth-child(even) td { background: #f7f7f7; }
  #results-table tr.settings-changed td { background: #e8e8e8 !important; }

  #results-table td.outcome-error      { color: #c0392b; }
  #results-table td.outcome-prompted   { color: #b7791f; }
  #results-table td.outcome-ok         { color: #27ae60; }
  #results-table td.outcome-correction { color: #6b46c1; }

  #print-summary {
    font-size: 9.5pt;
    color: #333;
    border-top: 1px solid #bbb;
    padding-top: 8px;
  }
  #print-summary span { margin-right: 20px; }
}

/* ── Responsive ────────────────────────────────────────────────────── */
@media (max-width: 680px) {
  :root { --tile-sz: 96px; --tile-gap: 8px; }

  /* Header */
  #app-header { padding: 8px 12px; gap: 8px; }
  #app-header h1 { font-size: 1.05rem; }
  #timer-display { font-size: 1.25rem; min-width: 62px; padding: 2px 8px; }
  .btn-timer { padding: 4px 9px; font-size: 0.76rem; }

  /* Settings bar */
  #settings-bar { padding: 6px 12px; gap: 8px; }
  .setting-group > label:first-child,
  .checkbox-label { font-size: 0.78rem; }
  .setting-group select,
  .setting-group input[type="number"] { font-size: 0.82rem; padding: 4px 6px; }

  /* Game area — column, anchored to top */
  #game-area {
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 16px;
    padding: 16px 12px;
  }

  /* Sample card */
  #sample-card { width: 150px; height: 150px; }
}

/* Narrow phones: shrink tiles further so 4-column grids still fit */
@media (max-width: 480px) {
  :root { --tile-sz: 72px; --tile-gap: 6px; }
  .ok-text { font-size: 2rem; }
}

/* ── Landscape phones: row layout so tiles fill horizontal space ────── */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --tile-sz: 100px; --tile-gap: 8px; }

  #game-area {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: center;
    gap: 14px;
    padding: 8px 14px;
  }

  #sample-section { flex-shrink: 0; }

  #sample-card {
    width: 110px;
    height: 110px;
  }

  #comp-section { flex: 1; min-width: 0; }
}

/* ── Mobile: lock viewport, header pinned, game area scrolls ──────── */
@media (max-width: 680px) {
  html, body {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    overscroll-behavior: contain;
  }
  body { min-height: 0; }

  #app-header { padding: 6px 10px; gap: 6px; flex-shrink: 0; }
  #app-header h1 { font-size: 0.95rem; }
  #timer-display { font-size: 1rem; min-width: 56px; padding: 2px 7px; }
  .btn-timer { padding: 3px 8px; font-size: 0.72rem; min-width: 0; }
  #btn-back { padding: 4px 9px; font-size: 0.76rem; }

  #settings-bar { padding: 6px 10px; gap: 6px 8px; flex-shrink: 0; }
  #extra-panel, #target-panel { flex-shrink: 0; }

  #game-area {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    padding: 10px 10px;
    gap: 10px;
    justify-content: safe center;
    align-items: center;
  }

  .area-label { margin-bottom: 4px; font-size: 0.68rem; }

  #sample-section { flex: 0 0 auto; }
  #sample-card { width: 96px; height: 96px; }

  #comp-section { flex: 0 0 auto; width: 100%; align-items: center; position: relative; }

  #comp-grid {
    width: auto;
    max-width: 100%;
  }

  #bottom-bar { padding: 6px 10px; gap: 8px; flex-shrink: 0; }
}

/* Landscape phones: row layout so sample sits beside the grid */
@media (orientation: landscape) and (max-height: 500px) {
  html, body {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    overscroll-behavior: contain;
  }
  body { min-height: 0; }

  #app-header { padding: 4px 10px; flex-shrink: 0; }
  #app-header h1 { font-size: 0.95rem; }
  #settings-bar { padding: 4px 10px; flex-shrink: 0; }

  #game-area {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding: 6px 10px;
    gap: 12px;
  }

  #sample-section { flex: 0 0 auto; }
  #sample-card { width: 80px; height: 80px; }

  #comp-section { flex: 0 0 auto; min-width: 0; align-items: center; }
  #comp-grid { width: auto; max-width: 100%; }
}

