/* === Color Palette === */

:root {
  --bg: #fafafa;
  --text: #2a2a2a;
  --text-muted: #707070;
  --text-faint: #737373;
  --text-muted-hover: #666;
  --text-bold: #000;
  --chrome: #ddd;
  --chrome-light: #eee;
  --surface: #f0f0f0;
  --surface-hover: #f5f5f5;
  --accent: #f5a623;
  --focus: #4a90d9;
  --error: #c0392b;
  --delta-pos: #22c55e;
  --delta-neg: #ef4444;
  --delta-stroke: rgba(0, 0, 0, 0.7);
  --overlay: rgba(0, 0, 0, 0.5);
}

/* === Skip Link === */

.skip-link {
  position: absolute;
  top: -100%;
  left: 16px;
  z-index: 200;
  padding: 8px 16px;
  background: var(--text);
  color: var(--bg);
  font-family: inherit;
  font-size: 14px;
  text-decoration: none;
}

.skip-link:focus {
  top: 8px;
}

/* === Reset & Base === */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Mono', 'JetBrains Mono', ui-monospace, 'Cascadia Code', 'Fira Code', 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

/* === App Shell === */

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100dvh;
  min-height: 100dvh;
  max-width: 400px;
  margin: 0 auto;
  padding: 12px 16px env(safe-area-inset-bottom, 8px);
  gap: 4px;
}

/* === Header === */

#header {
  width: 100%;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-shrink: 0;
  position: relative;
  z-index: 1;
}

.header-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

#puzzle-name {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

#puzzle-number {
  font-size: 12px;
  font-weight: 400;
  color: var(--text-muted);
}

#archive-link,
#latest-link {
  font-size: 11px;
  color: var(--text-faint);
  text-decoration: none;
  padding: 12px 8px;
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#archive-link:hover,
#latest-link:hover {
  color: var(--text-muted-hover);
}

/* === Game Area === */

#game-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  justify-content: safe center;
  width: 100%;
  min-height: 0;
  gap: 12px;
}

#game-area[hidden] {
  display: none;
}

/* === Grid === */

#grid-container {
  position: relative;
  width: 100%;
  max-width: 100%;
  aspect-ratio: 1;
  flex: 0 1 min(60vw, 340px);
  min-height: 0;
  max-height: min(60vw, 340px);
  display: flex;
  align-items: center;
  justify-content: center;
}

#grid-svg {
  width: 100%;
  height: 100%;
  display: block;
}

#grid-svg [role="gridcell"] > rect {
  fill: var(--surface);
  stroke: var(--chrome);
}

/* === Piece Info === */

#piece-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

#piece-info[hidden] {
  display: flex;
  visibility: hidden;
}

#piece-preview {
  width: 44px;
  height: 44px;
  display: block;
}

#pieces-left {
  font-size: 13px;
  color: var(--text-muted);
}

/* === Score Display === */

#score-display {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

#score-value {
  font-size: 32px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

#stars {
  display: flex;
  gap: 4px;
}

.star {
  font-size: 20px;
  color: var(--chrome);
  transition: color 0.3s ease;
}

.star.earned {
  color: var(--accent);
}

/* === Game End === */

#game-end {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 16px 0;
  flex-shrink: 0;
  visibility: hidden;
}

#game-end[hidden] {
  display: none;
}

#game-end.visible {
  visibility: visible;
}

#end-best-stars {
  display: flex;
  gap: 4px;
}

.end-label {
  font-size: 11px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#end-best {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  font-size: 13px;
}

#end-best-value {
  font-weight: 600;
}

#end-new-best {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  visibility: hidden;
}

#end-new-best.visible {
  visibility: visible;
}

#end-attempts {
  font-size: 12px;
  color: var(--text-muted);
}

#end-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

#play-again {
  appearance: none;
  -webkit-appearance: none;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  background: none;
  border: 1.5px solid var(--text);
  border-radius: 0;
  padding: 10px 28px;
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

#play-again:hover {
  background: var(--text);
  color: var(--bg);
}

#share-btn {
  appearance: none;
  -webkit-appearance: none;
  font-family: inherit;
  color: var(--text-muted);
  background: none;
  border: 1.5px solid var(--chrome);
  border-radius: 0;
  padding: 0;
  width: 44px;
  height: 44px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: color 0.15s ease, border-color 0.15s ease;
}

#share-btn:hover {
  color: var(--text);
  border-color: var(--text);
}

#share-btn.copied {
  color: var(--delta-pos);
  border-color: var(--delta-pos);
}

#share-btn .share-copied {
  position: absolute;
  top: -24px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: var(--delta-pos);
  white-space: nowrap;
  pointer-events: none;
}

/* === Error State === */

#error-state {
  text-align: center;
  padding: 24px 16px;
  flex-shrink: 0;
}

#error-state[hidden] {
  display: none;
}

#error-message {
  font-size: 13px;
  color: var(--error);
}

/* === Navigation Bar === */

#nav-bar {
  width: 100%;
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-shrink: 0;
  padding-top: 8px;
}

.nav-btn {
  appearance: none;
  -webkit-appearance: none;
  font-family: inherit;
  font-size: 16px;
  color: var(--text);
  background: none;
  border: none;
  border-radius: 0;
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s ease, background 0.15s ease;
}

.nav-btn:hover:not(:disabled) {
  color: var(--text-bold);
  background: var(--chrome-light);
}

.nav-btn:disabled {
  color: var(--chrome);
  cursor: default;
}

#nav-bar[hidden] {
  display: none;
}

/* === Focus Styles === */

:focus:not(:focus-visible) {
  outline: none;
}

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

/* === Score Delta Animation === */

@keyframes delta-pop-in {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes delta-float-fade {
  0% {
    transform: translateY(0);
    opacity: 1;
  }
  100% {
    transform: translateY(var(--delta-float-y, -20px));
    opacity: 0;
  }
}

.score-delta {
  font-weight: 700;
  pointer-events: none;
  transform-box: fill-box;
  transform-origin: center;
  stroke: var(--delta-stroke);
  stroke-width: 3;
  paint-order: stroke;
  stroke-linejoin: round;
  animation: delta-pop-in 120ms ease-out forwards, delta-float-fade 1700ms 120ms ease-out forwards;
}

.score-delta.positive {
  fill: var(--delta-pos);
}

.score-delta.negative {
  fill: var(--delta-neg);
}

/* === Artist's Statement Modal === */

#statement-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--overlay);
}

#statement-modal[hidden] {
  display: none;
}

#statement-content {
  position: relative;
  background: var(--bg);
  max-width: 340px;
  width: calc(100% - 48px);
  max-height: 80vh;
  overflow-y: auto;
  padding: 32px 24px 24px;
}

#statement-close {
  position: absolute;
  top: 8px;
  right: 8px;
  appearance: none;
  -webkit-appearance: none;
  font-family: inherit;
  font-size: 20px;
  color: var(--text-muted);
  background: none;
  border: none;
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#statement-close:hover {
  color: var(--text);
}

#statement-content:focus {
  outline: none;
}

#statement-body {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
  white-space: pre-line;
}

/* === Screen Reader Only === */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* === Ghost Piece Preview (desktop hover) === */

.ghost-piece {
  opacity: 0.25;
  pointer-events: none;
}

/* === Piece Placement Animation === */

.piece-icon {
  animation: piece-place-in 150ms ease-out forwards;
  transform-box: fill-box;
  transform-origin: center;
}

@keyframes piece-place-in {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* === Custom Puzzle Buttons === */

.custom-btn {
  font-family: inherit;
  font-size: 13px;
  color: var(--text);
  background: none;
  border: 1px solid var(--chrome);
  padding: 6px 12px;
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
}

/* === Archive View === */

#archive-view {
  flex: 1;
  width: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
}

#archive-view[hidden] {
  display: none;
}

.archive-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  padding: 24px 0;
}

.archive-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.archive-entry {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 10px 4px;
  text-decoration: none;
  color: inherit;
  border-bottom: 1px solid var(--chrome-light);
  min-height: 44px;
}

.archive-entry:hover {
  background: var(--surface-hover);
}

.archive-num {
  font-size: 12px;
  color: var(--text-muted);
  min-width: 32px;
}

.archive-name {
  flex: 1;
  font-size: 13px;
}

.archive-stars {
  display: flex;
  gap: 2px;
}

.archive-stars .star {
  font-size: 14px;
}

/* === Desktop Layout === */

@media (min-width: 500px) {
  #app {
    padding: 24px 16px 16px;
  }
}

/* === Small Screen Adjustments === */

@media (max-height: 600px) {
  #app {
    padding: 8px 12px 4px;
    gap: 0;
  }

  #score-value {
    font-size: 26px;
  }

  #grid-container {
    flex: 0 1 50vw;
    max-height: 50vw;
  }
}

/* === Reduced Motion === */

@media (prefers-reduced-motion: reduce) {
  .score-delta {
    animation: none;
    opacity: 1;
  }

  .piece-icon {
    animation: none;
    opacity: 1;
  }

  .star {
    transition: none;
  }

  #play-again {
    transition: none;
  }

  #share-btn {
    transition: none;
  }

  .nav-btn {
    transition: none;
  }
}

/* === Dark Mode === */

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --text: #e0e0e0;
    --text-muted: #999;
    --text-faint: #8a8a8a;
    --text-muted-hover: #bbb;
    --text-bold: #fff;
    --chrome: #444;
    --chrome-light: #333;
    --surface: #252525;
    --surface-hover: #2a2a2a;
    --focus: #5a9fd9;
    --error: #e74c3c;
    --delta-stroke: rgba(0, 0, 0, 0.9);
    --overlay: rgba(0, 0, 0, 0.7);
  }
}
