:root {
  --accent: rgb(204, 97, 71);
  --accent-soft: rgba(204, 97, 71, 0.16);
  --bg: #f5f3f0;
  --panel: #ffffff;
  --text: #1c1b1a;
  --text-secondary: #6b6864;
  --text-tertiary: #9b9893;
  --border: rgba(0, 0, 0, 0.10);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.14);
  --radius: 14px;
  font-synthesis: none;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1917;
    --panel: #262422;
    --text: #f0eee9;
    --text-secondary: #a8a49d;
    --text-tertiary: #767069;
    --border: rgba(255, 255, 255, 0.12);
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  }
}

* { box-sizing: border-box; }

/* `display: flex` on .modal/.overlay/.unlock-banner would otherwise win over
   the user-agent [hidden] rule, so guard it explicitly. */
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 32px 16px;
}

.app {
  width: 100%;
  max-width: 720px;
  background: var(--panel);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 19px; margin: 0; font-weight: 600; }

.brand-brick {
  width: 20px; height: 16px;
  background: var(--accent);
  border-radius: 3px;
  position: relative;
}
.brand-brick::before, .brand-brick::after {
  content: ""; position: absolute; top: -4px;
  width: 5px; height: 5px; border-radius: 2px; background: var(--accent);
}
.brand-brick::before { left: 3px; }
.brand-brick::after { right: 3px; }

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

.icon-btn {
  border: none; background: transparent; cursor: pointer;
  font-size: 16px; padding: 6px 8px; border-radius: 8px;
  color: var(--text-secondary); line-height: 1;
}
.icon-btn:hover { background: var(--accent-soft); }

/* World */
.world-wrap {
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: linear-gradient(to bottom, rgba(127,127,127,0.05), rgba(127,127,127,0.01));
}
.world-scroll { overflow-x: auto; overflow-y: hidden; }
#world-canvas { display: block; cursor: pointer; }

/* Unlock banner */
.unlock-banner {
  display: flex; align-items: center; gap: 10px;
  background: var(--accent-soft);
  border-radius: 10px; padding: 8px 12px;
}
.unlock-banner span:nth-child(3) { flex: 1; font-size: 14px; }
.unlock-spark { font-size: 15px; }

/* Controls */
.controls { display: flex; flex-direction: column; gap: 12px; }

.design-picker {
  display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px 8px;
}
.swatch {
  flex: 0 0 auto; display: flex; flex-direction: column; align-items: center;
  gap: 3px; cursor: pointer; border: none; background: transparent; padding: 0;
}
.swatch canvas {
  border-radius: 6px; padding: 2px;
  border: 2px solid transparent;
}
.swatch.selected canvas { border-color: var(--accent); }
.swatch.locked { cursor: not-allowed; }
.swatch.locked canvas { filter: grayscale(1); opacity: 0.35; }
.swatch-name { font-size: 11px; color: var(--text); }
.swatch.locked .swatch-name { color: var(--text-secondary); }
.swatch-lock { font-size: 10px; color: var(--text-tertiary); }

.duration-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.pill {
  border: none; cursor: pointer; font-size: 13px;
  padding: 5px 12px; border-radius: 999px;
  background: rgba(127,127,127,0.12); color: var(--text);
}
.pill.selected { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.duration-input {
  width: 48px; text-align: right; padding: 5px 7px; font-size: 13px;
  border: 1px solid var(--border); border-radius: 7px;
  background: var(--panel); color: var(--text); margin-left: auto;
}
.duration-row .unit { font-size: 13px; color: var(--text-secondary); }

.hint { font-size: 12px; color: var(--text-tertiary); text-align: center; }

.btn {
  border: none; cursor: pointer; font-size: 14px; font-weight: 500;
  padding: 9px 14px; border-radius: 9px;
  background: rgba(127,127,127,0.14); color: var(--text);
}
.btn:hover { background: rgba(127,127,127,0.22); }
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: rgb(184, 84, 60); }
.btn-danger { color: rgb(199, 70, 60); }
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }

.timer-display { text-align: center; }
.timer-time {
  font-size: 46px; font-weight: 500; letter-spacing: -1px;
  font-variant-numeric: tabular-nums;
  font-family: "SF Pro Rounded", -apple-system, system-ui, sans-serif;
}
.timer-sub { font-size: 12px; color: var(--text-tertiary); margin-top: 2px; }
.timer-head { display: flex; align-items: center; justify-content: center; gap: 12px; }
.timer-head canvas { border-radius: 4px; }

.edit-bar {
  display: flex; align-items: center; gap: 10px;
  background: rgba(127,127,127,0.08); border-radius: 10px; padding: 10px 12px;
}
.edit-bar .edit-label { flex: 1; font-size: 14px; }
.edit-bar canvas { border-radius: 4px; }

/* Modals */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  padding: 20px;
}
.modal-card {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  width: 100%; max-width: 440px; max-height: 86vh; overflow: hidden;
  display: flex; flex-direction: column;
}
.modal-card-wide { max-width: 620px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 18px; overflow-y: auto; }

.setting-group { margin-bottom: 22px; }
.setting-group h3 {
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px;
  color: var(--text-secondary); margin: 0 0 10px;
}
.setting-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 0; gap: 12px;
}
.setting-row label { font-size: 14px; }
.setting-row input[type="number"] {
  width: 64px; padding: 5px 7px; font-size: 14px; text-align: right;
  border: 1px solid var(--border); border-radius: 7px;
  background: var(--panel); color: var(--text);
}
.setting-row select {
  padding: 5px 8px; font-size: 14px; border: 1px solid var(--border);
  border-radius: 7px; background: var(--panel); color: var(--text);
}
.setting-note { font-size: 12px; color: var(--text-tertiary); margin: 2px 0 0; }

/* iOS-ish toggle */
.toggle { position: relative; width: 40px; height: 24px; flex: 0 0 auto; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .track {
  position: absolute; inset: 0; background: rgba(127,127,127,0.35);
  border-radius: 999px; transition: background 0.15s;
}
.toggle .track::before {
  content: ""; position: absolute; left: 2px; top: 2px;
  width: 20px; height: 20px; background: white; border-radius: 50%;
  transition: transform 0.15s; box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track::before { transform: translateX(16px); }

/* Stats */
.stat-cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 18px; }
.stat-cards.cards-3 { grid-template-columns: repeat(3, 1fr); }
.stat-cards.cards-2 { grid-template-columns: repeat(2, 1fr); }
.stat-card {
  background: rgba(127,127,127,0.08); border-radius: 10px;
  padding: 12px 8px; text-align: center;
}
.stat-value { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 11px; color: var(--text-secondary); margin-top: 3px; }
.stats-section { margin-bottom: 20px; }
.stats-section h3 { font-size: 14px; margin: 0 0 10px; }
.stats-section canvas { width: 100%; display: block; }
.progress-track {
  height: 8px; background: rgba(127,127,127,0.2); border-radius: 999px; overflow: hidden; margin-top: 8px;
}
.progress-fill { height: 100%; background: var(--accent); border-radius: 999px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-secondary); margin-top: 6px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

/* Completion overlay */
.overlay {
  position: fixed; inset: 0; pointer-events: none; z-index: 200;
  display: flex;
}
.overlay[data-position="corner"] { align-items: flex-start; justify-content: flex-end; padding: 16px; }
.overlay[data-position="center"] { align-items: center; justify-content: center; }
.overlay-card {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px 22px; text-align: center; opacity: 0;
}
.overlay.show .overlay-card { animation: overlay-in 0.25s ease forwards; }
.overlay.hide .overlay-card { animation: overlay-out 0.35s ease forwards; }
.overlay-art { height: 46px; display: flex; align-items: flex-end; justify-content: center; position: relative; }
.overlay-art canvas { position: relative; }
.overlay-text { font-size: 14px; font-weight: 600; margin-top: 8px; }

@keyframes overlay-in { from { opacity: 0; transform: scale(0.96); } to { opacity: 1; transform: scale(1); } }
@keyframes overlay-out { from { opacity: 1; } to { opacity: 0; } }

.drop-anim { animation: block-drop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) forwards; }
@keyframes block-drop {
  0% { transform: translateY(-40px) scaleY(0.85); }
  60% { transform: translateY(0) scaleY(1.12); }
  100% { transform: translateY(0) scaleY(1); }
}
.confetti {
  position: absolute; width: 5px; height: 5px; border-radius: 50%;
  left: 50%; bottom: 8px; opacity: 0.9;
}
.confetti.burst { animation: confetti-burst 0.7s ease-out forwards; }
@keyframes confetti-burst { to { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .drop-anim, .confetti.burst, .overlay.show .overlay-card, .overlay.hide .overlay-card { animation: none; }
  .overlay.show .overlay-card { opacity: 1; }
  .confetti { display: none; }
}
