/* Toy-town play mat: grass-green canvas, paper sidebar, one Tomica-red accent.
   Rounded JP system faces keep the toy-package feel with zero dependencies. */

:root {
  --mat: #7fb069;
  --mat-dot: #6f9e5b;
  --paper: #fbf8f1;
  --paper-edge: #e7e0d2;
  --ink: #26313c;
  --ink-soft: #6b7480;
  --red: #e60012;
  --red-press: #c1000f;
  --sign-green: #1e7f4f;
  --sign-amber: #c77700;
}

* { box-sizing: border-box; margin: 0; }

html, body { height: 100%; }

body {
  display: flex;
  font-family: ui-rounded, "Hiragino Maru Gothic ProN", "Arial Rounded MT Bold",
    "Yu Gothic", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

/* --- sidebar ------------------------------------------------------------ */

.panel {
  flex: 0 0 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px 16px;
  background: var(--paper);
  border-right: 2px solid var(--paper-edge);
  overflow-y: auto;
}

.masthead-plate {
  background: var(--red);
  color: #fff;
  border-radius: 14px;
  padding: 12px 16px 10px;
  box-shadow: 0 3px 0 var(--red-press);
}

.masthead-plate h1 {
  font-size: 1.45rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.masthead-jp {
  font-size: 0.72rem;
  opacity: 0.9;
  letter-spacing: 0.12em;
}

.masthead-hint {
  margin-top: 10px;
  font-size: 0.82rem;
  color: var(--ink-soft);
  line-height: 1.45;
}

/* --- piece rows --------------------------------------------------------- */

.piece-rows {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.piece-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  background: #fff;
  border: 1.5px solid var(--paper-edge);
  border-radius: 12px;
}

.piece-icon {
  flex: 0 0 58px;
  height: 44px;
  background: var(--mat);
  border-radius: 8px;
  padding: 4px;
  display: flex;
}

.piece-icon svg { width: 100%; height: 100%; }

.piece-name {
  flex: 1;
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.piece-name span { font-size: 0.9rem; font-weight: 600; }
.piece-name small { font-size: 0.68rem; color: var(--ink-soft); }

.stepper { display: flex; align-items: center; gap: 2px; }

.stepper .step {
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 9px;
  background: var(--paper-edge);
  color: var(--ink);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.stepper .step:hover { background: #d9d1bf; }

.stepper input {
  width: 46px;
  height: 30px;
  border: 1.5px solid var(--paper-edge);
  border-radius: 9px;
  text-align: center;
  font: inherit;
  font-weight: 700;
  color: var(--ink);
  background: #fff;
}

.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; }
.stepper input[type="number"] { -moz-appearance: textfield; appearance: textfield; }

/* --- buttons ------------------------------------------------------------ */

.btn {
  font: inherit;
  font-weight: 700;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  padding: 10px 14px;
}

.btn:focus-visible { outline: 3px solid #7ab8ff; outline-offset: 2px; }

.btn-quiet {
  flex: 1;
  background: #fff;
  border: 1.5px solid var(--paper-edge);
  color: var(--ink);
  font-size: 0.78rem;
  white-space: nowrap;
  padding: 10px 8px;
}

.btn-quiet:hover { border-color: var(--ink-soft); }

.generate-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 6px;
}

.options {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #fff;
  border: 1.5px solid var(--paper-edge);
  border-radius: 12px;
  padding: 10px 12px;
}

.opt-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.opt-row input[type="checkbox"] {
  width: 17px;
  height: 17px;
  accent-color: var(--sign-green);
  order: 2;
}

.opt-row select,
.opt-row input[type="number"] {
  font: inherit;
  font-size: 0.8rem;
  border: 1.5px solid var(--paper-edge);
  border-radius: 8px;
  padding: 3px 6px;
  background: #fff;
  color: var(--ink);
}

.opt-row input[type="number"] { width: 52px; text-align: right; }

.pct { display: flex; align-items: center; gap: 3px; color: var(--ink-soft); }

.loop-hint {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--sign-amber);
  background: #fdf3e3;
  border: 1.5px solid #eeddc0;
  border-radius: 10px;
  padding: 8px 10px;
  line-height: 1.4;
}

.btn-go {
  background: var(--red);
  color: #fff;
  font-size: 1.05rem;
  padding: 14px;
  border-radius: 14px;
  box-shadow: 0 3px 0 var(--red-press);
}

.btn-go:hover:not(:disabled) { transform: translateY(-1px); }
.btn-go:active:not(:disabled) { transform: translateY(1px); box-shadow: none; }
.btn-go:disabled { opacity: 0.6; cursor: wait; }

.seed {
  font-size: 0.68rem;
  color: var(--ink-soft);
  text-align: center;
  min-height: 1em;
  font-variant-numeric: tabular-nums;
}

/* --- play mat ----------------------------------------------------------- */

.mat {
  flex: 1;
  position: relative;
  background: var(--mat);
  background-image: radial-gradient(var(--mat-dot) 2.2px, transparent 2.2px);
  background-size: 34px 34px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#canvas {
  flex: 1;
  width: 100%;
  touch-action: none;
  cursor: grab;
}

#canvas:active { cursor: grabbing; }

body.busy #canvas { opacity: 0.55; transition: opacity 0.15s; }

.stats {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  z-index: 2;
  pointer-events: none;
}

.badge, .chip {
  font-size: 0.74rem;
  font-weight: 700;
  padding: 5px 11px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  color: var(--ink);
}

/* round road-sign badge for a closed loop */
.badge-loop {
  background: var(--sign-green);
  color: #fff;
  border: 2.5px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}

.badge-open {
  background: var(--sign-amber);
  color: #fff;
  border: 2.5px solid #fff;
}

.badge-info { background: rgba(255, 255, 255, 0.92); }

.badge-back {
  pointer-events: auto;
  cursor: pointer;
  border: 1.5px solid var(--paper-edge);
  background: #fff;
  font-family: inherit;
}

.badge-back:hover { border-color: var(--ink-soft); }

.chip-left { color: var(--sign-amber); }

.reason {
  flex-basis: 100%;
  font-size: 0.78rem;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  text-align: center;
  padding: 24px;
}

.empty[hidden] { display: none; }

.empty-sign {
  width: 74px;
  height: 74px;
  border-radius: 50%;
  background: var(--sign-green);
  border: 5px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  color: rgba(255, 255, 255, 0.35);
  font-size: 1.6rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty p {
  max-width: 300px;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 0.9rem;
  font-weight: 600;
}

.mat-help {
  position: absolute;
  bottom: 10px;
  right: 14px;
  font-size: 0.68rem;
  color: rgba(255, 255, 255, 0.85);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  pointer-events: none;
}

/* --- gallery ------------------------------------------------------------ */

.gallery {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  padding: 16px;
  z-index: 3;
}

.gallery[hidden] { display: none; }

body.gallery-mode #canvas,
body.gallery-mode .stats,
body.gallery-mode .mat-help { display: none; }

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 14px;
  align-content: start;
}

.card {
  background: var(--paper);
  border-radius: 14px;
  padding: 8px;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.18);
  transition: transform 0.12s;
}

.card:hover { transform: translateY(-2px); box-shadow: 0 4px 10px rgba(0, 0, 0, 0.22); }

.card svg {
  width: 100%;
  height: 175px;
  background: var(--mat);
  border-radius: 10px;
  display: block;
}

.card-meta {
  display: flex;
  gap: 6px;
  padding-top: 7px;
}

.card-badge {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
  background: #fff;
  border: 1.5px solid var(--paper-edge);
  color: var(--ink);
}

.card-loop { background: var(--sign-green); border-color: var(--sign-green); color: #fff; }
.card-open { background: var(--sign-amber); border-color: var(--sign-amber); color: #fff; }

.gallery-note {
  text-align: center;
  padding: 18px;
  font-size: 0.85rem;
  font-weight: 600;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.gallery-sentinel { height: 2px; }

@media (prefers-reduced-motion: reduce) {
  .card, .card:hover { transform: none; transition: none; }
}

/* --- small screens ------------------------------------------------------ */

@media (max-width: 760px) {
  body { flex-direction: column; }
  .panel {
    flex: none;
    max-height: 52vh;
    border-right: none;
    border-bottom: 2px solid var(--paper-edge);
    padding: 12px;
    gap: 10px;
  }
  .mat { min-height: 48vh; }

  .masthead-plate { padding: 8px 12px 6px; }
  .masthead-plate h1 { font-size: 1.1rem; }
  .masthead-hint { display: none; }

  /* Generate stays reachable while the piece list and options scroll. */
  .generate-block {
    position: sticky;
    bottom: 0;
    background: var(--paper);
    padding-bottom: 4px;
    box-shadow: 0 -8px 12px -8px rgba(0, 0, 0, 0.15);
  }
  .btn-go { padding: 12px; font-size: 1rem; }

  /* 16px inputs stop iOS from zooming the page on focus. */
  .stepper input,
  .opt-row input[type="number"],
  .opt-row select { font-size: 16px; }
  .stepper .step { width: 34px; height: 34px; }
  .stepper input { width: 52px; height: 34px; }

  .gallery { padding: 10px; }
  .gallery-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 10px;
  }
  .card svg { height: 128px; }
  .card-meta { flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
