:root {
  color-scheme: light;
  /* Neutrals */
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efebe4;
  --border: #e0d9ce;
  --text: #1f1b16;
  --text-muted: #6b6257;
  /* Accent + semantics */
  --accent: #d9480f;
  --accent-soft: #fde7dc;
  --success: #2b8a3e;
  --success-soft: #e3f4e6;
  --danger: #c92a2a;
  --danger-soft: #fbe4e4;
  --warning: #a35f00;
  --warning-soft: #fdf0d8;

  /* Type scale */
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-display: ui-rounded, "SF Pro Rounded", "Nunito", "Varela Round", var(--font-body);
  --fs-xs: 0.75rem;
  --fs-sm: 0.875rem;
  --fs-md: 1rem;
  --fs-lg: 1.25rem;
  --fs-xl: 1.75rem;
  --fs-2xl: 2.5rem;

  /* Spacing (4px base) */
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 24px;
  --s-6: 32px;
  --s-7: 48px;

  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 22px;
  --shadow: 0 1px 2px rgb(31 27 22 / 6%), 0 4px 16px rgb(31 27 22 / 8%);
  --shadow-lift: 0 2px 4px rgb(31 27 22 / 10%), 0 12px 28px rgb(31 27 22 / 18%);
  --tile: 104px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #15130f;
    --surface: #1f1c17;
    --surface-2: #2a2620;
    --border: #3a342c;
    --text: #f3eee6;
    --text-muted: #a89e90;
    --accent: #ff8a4c;
    --accent-soft: #3d2416;
    --success: #69db7c;
    --success-soft: #1d3322;
    --danger: #ff8787;
    --danger-soft: #3d1f1f;
    --warning: #ffc078;
    --warning-soft: #3a2c16;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 30%);
    --shadow-lift: 0 2px 4px rgb(0 0 0 / 40%), 0 12px 28px rgb(0 0 0 / 50%);
  }
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: var(--fs-md)/1.5 var(--font-body);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p { margin: 0; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Layout: two halves ---------- */

.layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Left: the mosaic ---------- */

.gallery {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
}

.gallery__bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--s-3);
  padding: var(--s-4) var(--s-5);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.gallery__title {
  font-family: var(--font-display);
  font-size: var(--fs-lg);
  font-weight: 800;
  letter-spacing: -0.01em;
}

.gallery__count {
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

.gallery__tools {
  display: flex;
  gap: var(--s-2);
}

.input, .button {
  height: 36px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: var(--fs-sm);
}

.input {
  width: 200px;
  max-width: 100%;
  padding: 0 var(--s-3);
}

.button {
  padding: 0 var(--s-4);
  font-weight: 600;
  cursor: pointer;
}

.button:hover { background: var(--surface-2); }

.mosaic {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--s-4) var(--s-5) var(--s-6);
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--tile), 1fr));
  grid-auto-rows: var(--tile);
  grid-auto-flow: dense;
  gap: var(--s-2);
  align-content: start;
}

.tile {
  position: relative;
  padding: 0;
  border: 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-2);
  cursor: grab;
  transition: transform 140ms ease, box-shadow 140ms ease;
}

.tile--wide { grid-column: span 2; }
.tile--tall { grid-row: span 2; }
.tile--big { grid-column: span 2; grid-row: span 2; }

.tile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.tile:hover, .tile:focus-visible {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lift);
  z-index: 1;
}

.tile:active { cursor: grabbing; }

.tile.is-dragging { opacity: 0.35; }

.tile__name {
  position: absolute;
  inset: auto 0 0 0;
  padding: var(--s-5) var(--s-2) var(--s-2);
  background: linear-gradient(transparent, rgb(0 0 0 / 78%));
  color: #fff;
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: 1.25;
  text-align: left;
  opacity: 0;
  transition: opacity 140ms ease;
}

.tile:hover .tile__name, .tile:focus-visible .tile__name { opacity: 1; }

.tile__badge {
  position: absolute;
  top: var(--s-1);
  right: var(--s-1);
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: var(--fs-xs);
  font-weight: 800;
  color: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 40%);
}

.tile__badge--ok { background: var(--success); }
.tile__badge--miss { background: var(--danger); }

.mosaic__empty {
  grid-column: 1 / -1;
  padding: var(--s-6) 0;
  color: var(--text-muted);
  text-align: center;
}

/* ---------- Right: drop zone + result ---------- */

.panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: var(--s-5);
  gap: var(--s-4);
}

.dropzone {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  transition: border-color 140ms ease, background 140ms ease;
}

.dropzone.is-over {
  border-color: var(--accent);
  border-style: solid;
  background: var(--accent-soft);
}

.dropzone[data-state="empty"] .result { display: none; }
.dropzone:not([data-state="empty"]) .dropzone__empty { display: none; }
.dropzone:not([data-state="empty"]) { border-style: solid; }

.dropzone__empty {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  padding: var(--s-6);
  text-align: center;
}

.dropzone__icon { color: var(--accent); margin-bottom: var(--s-2); }

.dropzone__lead {
  font-family: var(--font-display);
  font-size: var(--fs-xl);
  font-weight: 800;
}

.dropzone__sub {
  max-width: 36ch;
  color: var(--text-muted);
  font-size: var(--fs-sm);
}

.result {
  display: grid;
  grid-template-columns: minmax(120px, 200px) 1fr;
  gap: var(--s-5);
  padding: var(--s-5);
  align-items: start;
}

.result__image {
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  border-radius: var(--radius);
  background: var(--surface-2);
  box-shadow: var(--shadow);
}

.result__body {
  display: flex;
  flex-direction: column;
  gap: var(--s-4);
  min-width: 0;
}

.eyebrow {
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.result__name {
  font-size: var(--fs-sm);
  color: var(--text-muted);
}

.result__name strong { color: var(--text); }

.result__studio {
  font-family: var(--font-display);
  font-size: var(--fs-2xl);
  font-weight: 800;
  line-height: 1.05;
  letter-spacing: -0.02em;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s-2);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--s-1);
  padding: 2px var(--s-3);
  border-radius: 999px;
  font-size: var(--fs-xs);
  font-weight: 700;
  background: var(--surface-2);
  color: var(--text-muted);
}

.chip--ok { background: var(--success-soft); color: var(--success); }
.chip--miss { background: var(--danger-soft); color: var(--danger); }
.chip--warn { background: var(--warning-soft); color: var(--warning); }

.ranking {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--s-2);
}

.ranking__row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 3.5rem;
  gap: var(--s-1) var(--s-3);
  font-size: var(--fs-sm);
}

.ranking__label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ranking__value { text-align: right; font-variant-numeric: tabular-nums; color: var(--text-muted); }

.ranking__bar {
  grid-column: 1 / -1;
  height: 6px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}

.ranking__bar span {
  display: block;
  height: 100%;
  border-radius: 3px;
  background: var(--text-muted);
  transform-origin: left;
  animation: grow 500ms ease-out both;
}

.ranking__row:first-child .ranking__bar span { background: var(--accent); }

@keyframes grow { from { transform: scaleX(0); } }

.facts {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: var(--s-1) var(--s-4);
  margin: 0;
  font-size: var(--fs-sm);
}

.facts dt { color: var(--text-muted); }
.facts dd { margin: 0; }

.result__meta { font-size: var(--fs-xs); color: var(--text-muted); }

/* Loading */
.loading {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  color: var(--text-muted);
  font-size: var(--fs-sm);
}

.spinner {
  width: 20px;
  height: 20px;
  border: 3px solid var(--surface-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.error {
  padding: var(--s-3) var(--s-4);
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
  font-size: var(--fs-sm);
}

/* History */
.history__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: var(--s-2);
}

.history__title { font-size: var(--fs-sm); font-weight: 700; }
.history__score { font-size: var(--fs-sm); color: var(--text-muted); font-variant-numeric: tabular-nums; }

.history__list {
  list-style: none;
  margin: 0;
  padding: 0 0 var(--s-1);
  display: flex;
  gap: var(--s-2);
  overflow-x: auto;
  min-height: 64px;
}

.history__item button {
  position: relative;
  width: 48px;
  height: 64px;
  padding: 0;
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-2);
  cursor: pointer;
}

.history__item button[aria-current="true"] { border-color: var(--accent); }
.history__item img { width: 100%; height: 100%; object-fit: cover; display: block; }

.history__item .tile__badge { width: 16px; height: 16px; font-size: 10px; top: 2px; right: 2px; }

.history__empty { font-size: var(--fs-sm); color: var(--text-muted); align-self: center; }

/* ---------- Phone / narrow: stack, tap to classify ---------- */

@media (max-width: 800px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 55dvh auto;
    height: auto;
  }

  .gallery { border-right: 0; border-bottom: 1px solid var(--border); min-height: 0; }
  .gallery__bar { padding: var(--s-3) var(--s-4); }
  .gallery__tools { width: 100%; }
  .input { flex: 1; width: auto; }
  .mosaic { padding: var(--s-3) var(--s-4) var(--s-4); --tile: 84px; }
  .panel { padding: var(--s-4); }
  .dropzone { min-height: 260px; }
  .dropzone__lead { font-size: var(--fs-lg); }
  .result { grid-template-columns: 96px 1fr; gap: var(--s-4); padding: var(--s-4); }
  .result__studio { font-size: var(--fs-xl); }
}

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