:root {
  --cocoa: #4b2e2a;
  --cocoa-2: #654039;
  --caramel: #9a6846;
  --honey: #d8a24c;
  --cream: #fff4c7;
  --paper: #fffdf5;
  --canvas: #f4efe8;
  --mint: #c5f59b;
  --green: #4fae34;
  --blue: #4296d2;
  --red: #c94a43;
  --ink: #2e2421;
  --muted: #796963;
  --line: #ded2c5;
  --line-strong: #cbb7a5;
  --shadow: 0 12px 34px rgba(75, 46, 42, .09);
  --label-width: 238px;
  --column-width: 230px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; color: var(--ink); background: var(--canvas); font: 14px/1.45 Inter, "Segoe UI", sans-serif; }
button, input, textarea, select { font: inherit; }
button { border: 1px solid var(--line-strong); border-radius: 9px; padding: 8px 12px; color: var(--cocoa); background: white; font-weight: 700; cursor: pointer; transition: border-color .15s, background .15s, transform .15s; }
button:hover { border-color: var(--cocoa); background: #fffaf0; }
button:active { transform: translateY(1px); }
button:disabled { opacity: .55; cursor: wait; }
button.primary { color: white; border-color: var(--green); background: var(--green); }
button.primary:hover { background: #438f31; }
button.ghost { color: #eadfd9; border-color: transparent; background: transparent; }
button.wide { width: 100%; }
button.icon-button { width: 34px; height: 34px; padding: 0; border-radius: 50%; font-size: 20px; }
input, textarea, select { width: 100%; border: 1px solid var(--line-strong); border-radius: 8px; background: white; color: var(--ink); padding: 8px 9px; outline: 0; }
input:focus, textarea:focus, select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(66,150,210,.13); }
label { display: grid; gap: 5px; color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .025em; }
h1, h2, p { margin-top: 0; }
h2 { margin: 1px 0 2px; color: var(--cocoa); font-size: 21px; letter-spacing: -.02em; }

.app { min-height: 100vh; display: grid; grid-template: 68px 1fr / 205px minmax(680px, 1fr) 350px; }
.topbar { grid-column: 1 / -1; position: sticky; top: 0; z-index: 30; display: flex; align-items: center; justify-content: space-between; min-width: 0; padding: 9px 16px; color: white; background: var(--cocoa); box-shadow: 0 4px 18px rgba(75,46,42,.18); }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { display: grid; width: 38px; height: 38px; place-items: center; border: 1px solid rgba(255,255,255,.25); border-radius: 10px; color: var(--cream); background: rgba(255,255,255,.08); font-weight: 900; }
.brand strong { display: block; font-size: 17px; line-height: 1.15; }
.eyebrow { display: block; color: var(--honey); font-size: 9px; font-weight: 900; letter-spacing: .15em; }
.eyebrow.dark { color: var(--caramel); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.save-state { display: flex; align-items: center; gap: 7px; margin-right: 4px; color: #e8d9d2; font-size: 12px; }
.save-state i { width: 7px; height: 7px; border-radius: 50%; background: #8fc77d; }
.publish-button { min-width: 142px; }
.export-button { min-width: 135px; color: var(--cream); border-color: rgba(255,255,255,.28); background: rgba(255,255,255,.08); }
.export-button:hover { color: var(--cocoa); border-color: var(--cream); background: var(--cream); }

.sidebar { position: sticky; top: 68px; height: calc(100vh - 68px); padding: 16px 12px; background: var(--cocoa-2); border-right: 1px solid rgba(75,46,42,.35); overflow-y: auto; }
.sidebar-title { margin: 22px 7px 8px; color: #d6bdb3; font-size: 10px; font-weight: 900; text-transform: uppercase; letter-spacing: .14em; }
.level-list { display: grid; gap: 4px; }
.level-item { width: 100%; text-align: left; color: #f5e9e3; border-color: transparent; background: transparent; }
.level-item:hover { border-color: rgba(255,255,255,.14); background: rgba(255,255,255,.06); }
.level-item.active { color: var(--cocoa); border-color: var(--honey); background: var(--cream); }
.level-item small { display: flex; align-items: center; justify-content: space-between; margin-top: 1px; opacity: .68; font-weight: 500; }
.level-item small::after { content: "Live"; padding: 1px 5px; border-radius: 8px; color: #d9f5d1; background: rgba(51,126,34,.55); font-size: 8px; font-weight: 800; text-transform: uppercase; letter-spacing: .07em; }
.level-item.pending small::after { content: "New"; color: var(--cocoa); background: var(--honey); }
.level-item.unpublished small::after { content: "Not live"; color: #f6e2d7; background: rgba(120,75,62,.7); }

.workspace { min-width: 0; padding: 18px; overflow: hidden; }
.panel { margin-bottom: 16px; border: 1px solid var(--line); border-radius: 14px; background: rgba(255,253,245,.97); box-shadow: var(--shadow); }
.level-header { display: grid; grid-template-columns: minmax(300px, 1fr) 250px auto; gap: 18px; align-items: center; padding: 14px; }
.level-fields { display: flex; gap: 12px; }
.field.grow { flex: 1; }
.id-field { width: 145px; }
.level-preview-block { display: flex; align-items: center; gap: 9px; }
.level-preview { display: grid; width: 78px; height: 56px; flex: 0 0 auto; place-items: center; overflow: hidden; border: 1px dashed var(--line-strong); border-radius: 8px; background: #eee7df; }
.level-preview img { width: 100%; height: 100%; object-fit: contain; }
.level-preview .empty { padding: 4px; color: var(--muted); font-size: 9px; text-align: center; }
.text-upload { display: inline-block; color: var(--blue); cursor: pointer; }
.text-upload input, .asset-upload input, .compact-upload input { display: none; }
.icon-danger { color: var(--red); border-color: transparent; background: transparent; }
.icon-danger:hover { border-color: #e6c6c2; background: #fff3f1; }

.matrix-panel { padding: 18px; }
.section-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 18px; }
.section-heading p { margin: 3px 0 0; color: var(--muted); font-size: 13px; }
.section-actions { display: flex; flex: 0 0 auto; gap: 8px; }
.matrix-legend { display: flex; align-items: center; gap: 18px; margin: 15px 0 10px; padding: 9px 11px; border-radius: 8px; color: var(--muted); background: #f6f0e7; font-size: 11px; }
.matrix-legend span { display: flex; align-items: center; gap: 6px; }
.matrix-legend span:last-child { margin-left: auto; }
.matrix-legend i { width: 10px; height: 10px; border-radius: 3px; }
.legend-result { background: var(--honey); }
.legend-answer { background: var(--blue); }
.matrix-scroll { overflow: auto; border: 1px solid var(--line-strong); border-radius: 11px; background: white; }
.matrix { min-width: 928px; }
.matrix-grid { display: grid; grid-template-columns: var(--label-width) repeat(var(--column-count, 3), minmax(var(--column-width), 1fr)); }
.matrix-corner, .column-editor, .row-label, .answer-cell { border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.matrix-corner { position: sticky; left: 0; z-index: 8; display: flex; flex-direction: column; justify-content: end; min-height: 265px; padding: 16px; color: var(--muted); background: #f7f1e8; }
.matrix-corner strong { color: var(--cocoa); font-size: 16px; }
.matrix-corner span { margin-top: 4px; font-size: 11px; }
.column-editor { position: relative; min-width: 0; min-height: 265px; padding: 11px; background: #fffdf8; }
.column-editor .column-title { padding-right: 31px; font-weight: 800; }
.column-editor .remove-column { position: absolute; z-index: 2; top: 13px; right: 13px; width: 26px; height: 26px; padding: 0; color: var(--red); border-color: transparent; background: transparent; }
.result-label { display: flex; align-items: center; justify-content: space-between; margin: 9px 1px 5px; color: var(--muted); font-size: 9px; font-weight: 900; letter-spacing: .09em; text-transform: uppercase; }
.result-label span { color: var(--green); letter-spacing: 0; text-transform: none; }
.result-composition { position: relative; width: 112px; max-width: 80%; aspect-ratio: 4 / 5; margin: 0 auto 8px; overflow: hidden; border: 1px solid var(--line); border-radius: 9px; background: repeating-conic-gradient(#eee9e3 0 25%, #faf7f2 0 50%) 50% / 14px 14px; }
.result-composition img, .composition-preview img { position: absolute; object-fit: contain; pointer-events: none; user-select: none; }
.result-composition .base-layer, .composition-preview .base-layer { inset: 0; width: 100%; height: 100%; }
.result-composition .placed-layer, .composition-preview .placed-layer { width: 55%; left: 50%; top: 50%; transform-origin: center; }
.composition-placeholder { position: absolute; inset: 0; display: grid; place-items: center; padding: 9px; color: var(--muted); font-size: 10px; text-align: center; }
.asset-upload { display: block; text-align: center; padding: 6px; border: 1px dashed var(--line-strong); border-radius: 7px; color: var(--blue); background: #fbfaf7; cursor: pointer; }

.row-label { position: sticky; left: 0; z-index: 7; min-width: 0; padding: 11px; background: #fbf4df; }
.row-title-line { display: flex; align-items: center; gap: 7px; }
.row-title { min-width: 0; font-weight: 800; }
.remove-row { flex: 0 0 auto; width: 28px; height: 28px; padding: 0; color: var(--red); border-color: transparent; background: transparent; }
.choice-caption { margin: 10px 0 5px; color: var(--muted); font-size: 9px; font-weight: 900; letter-spacing: .08em; text-transform: uppercase; }
.choice-stack { display: grid; gap: 5px; }
.choice-item { display: grid; grid-template-columns: 34px minmax(0,1fr) 25px; gap: 6px; align-items: center; }
.choice-thumb { width: 34px; height: 34px; object-fit: contain; overflow: hidden; border: 1px solid var(--line); border-radius: 6px; background: white; }
.choice-thumb.empty { display: grid; place-items: center; color: #a28e83; font-size: 8px; }
.choice-label { min-width: 0; padding: 6px 7px; font-size: 11px; }
.choice-tools { position: relative; }
.compact-upload { display: grid; width: 25px; height: 25px; place-items: center; border: 1px solid var(--line); border-radius: 6px; color: var(--blue); background: white; cursor: pointer; font-size: 13px; }
.remove-choice { position: absolute; top: -7px; right: -7px; display: none; width: 17px; height: 17px; padding: 0; border: 0; border-radius: 50%; color: white; background: var(--red); font-size: 11px; line-height: 1; }
.choice-item:hover .remove-choice { display: block; }
.add-choice { width: 100%; margin-top: 7px; padding: 5px; border-style: dashed; color: var(--caramel); background: rgba(255,255,255,.55); font-size: 11px; }
.answer-cell { min-width: 0; padding: 11px; background: white; }
.answer-card { height: 100%; min-height: 155px; display: flex; flex-direction: column; align-items: center; padding: 9px; border: 1px solid var(--line); border-radius: 9px; background: #fcfaf6; }
.answer-card:hover { border-color: #bba690; }
.answer-kicker { align-self: stretch; color: var(--muted); font-size: 9px; font-weight: 900; letter-spacing: .08em; text-transform: uppercase; }
.answer-thumb { display: grid; width: 67px; height: 67px; margin: 6px auto; place-items: center; object-fit: contain; color: #a48f83; border-radius: 8px; background: white; font-size: 9px; }
.answer-card select { padding: 6px 7px; font-size: 11px; }
.adjust-placement { width: 100%; margin-top: 7px; padding: 6px; color: var(--blue); border-color: #bdd7e8; background: #f4faff; font-size: 10px; }
.adjust-placement.active { color: white; border-color: var(--blue); background: var(--blue); }
.clues-panel { padding: 18px; }
.clues-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 10px; margin-top: 14px; }
.clue-card { position: relative; padding: 11px; border: 1px solid #d8bd89; border-radius: 10px; background: #fff8d9; }
.clue-card textarea { min-height: 90px; resize: vertical; border-color: transparent; background: rgba(255,255,255,.46); }
.clue-card textarea:focus { border-color: var(--blue); }
.clue-number { margin-bottom: 5px; color: var(--caramel); font-size: 9px; font-weight: 900; letter-spacing: .1em; }
.clue-target-label { display: grid; gap: 5px; margin-top: 8px; color: var(--muted); font-size: 9px; font-weight: 900; letter-spacing: .08em; text-transform: uppercase; }
.clue-target { width: 100%; padding: 7px; color: var(--cocoa); font-size: 11px; font-weight: 600; letter-spacing: 0; text-transform: none; }
.clue-card .remove-clue { margin-top: 5px; padding: 4px 6px; color: var(--red); border-color: transparent; background: transparent; font-size: 10px; }

.studio { position: sticky; top: 68px; height: calc(100vh - 68px); padding: 17px; border-left: 1px solid var(--line); background-color: var(--paper); background-image: linear-gradient(rgba(255,253,245,.94),rgba(255,253,245,.94)), url("assets/handmade_texture.png"); background-size: auto, 580px; overflow-y: auto; }
.studio-heading { display: flex; align-items: start; justify-content: space-between; }
.section-help { margin: 6px 0 13px; color: var(--muted); font-size: 12px; }
.composition-preview { position: relative; width: min(100%, 292px); aspect-ratio: 4 / 5; margin: 0 auto; overflow: hidden; border: 2px solid var(--cocoa); border-radius: 12px; background: repeating-conic-gradient(#e9e2dc 0 25%,#f7f3ef 0 50%) 50% / 18px 18px; touch-action: none; }
.composition-preview .placed-layer.selected { filter: drop-shadow(0 0 4px var(--blue)); outline: 2px dashed var(--blue); pointer-events: auto; cursor: move; }
.empty-preview { position: absolute; inset: 0; display: grid; place-items: center; padding: 20px; color: var(--muted); font-weight: 800; text-align: center; }
.placement-controls { display: grid; gap: 10px; margin-top: 13px; }
.drag-help { margin: 0; padding: 8px; border-radius: 7px; color: #356f94; background: #eaf6fd; font-size: 11px; }
.placement-controls label { grid-template-columns: 70px 1fr 43px; align-items: center; }
.placement-controls label small { grid-column: 2 / -1; }
.placement-controls input[type="number"] { max-width: 80px; }
.placement-controls output { text-align: right; color: var(--cocoa); }

.auth-screen { position: fixed; z-index: 100; inset: 0; display: grid; place-items: center; padding: 20px; background: #654039; }
.auth-card { width: min(420px, 100%); padding: 27px; border: 1px solid var(--line); border-radius: 17px; background: var(--paper); box-shadow: 0 20px 60px rgba(0,0,0,.24); }
.auth-card label { margin: 12px 0; }
.button-row { display: flex; gap: 9px; }
.error { min-height: 20px; margin: 12px 0 0; color: var(--red); font-weight: 700; }
.account-note { margin: 12px 0 0; color: var(--muted); font-size: 11px; }
.toast { position: fixed; z-index: 200; right: 18px; bottom: 18px; max-width: 430px; padding: 11px 16px; border-radius: 9px; color: white; background: var(--cocoa); box-shadow: var(--shadow); opacity: 0; transform: translateY(12px); pointer-events: none; transition: .2s; }
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1320px) {
  .app { grid-template-columns: 190px minmax(640px, 1fr); }
  .studio { position: fixed; z-index: 40; right: 0; top: 68px; width: 350px; box-shadow: -12px 0 35px rgba(75,46,42,.18); transform: translateX(103%); transition: transform .2s; }
  .studio.open { transform: translateX(0); }
}
@media (max-width: 860px) {
  .app { display: block; padding-top: 62px; }
  .topbar { position: fixed; inset: 0 0 auto; height: 62px; }
  .brand-mark, .topbar .save-state, #sign-out { display: none; }
  .sidebar { position: static; width: 100%; height: auto; display: flex; gap: 8px; overflow-x: auto; }
  .sidebar > .wide { width: auto; flex: 0 0 auto; }
  .sidebar-title { display: none; }
  .level-list { display: flex; }
  .level-item { min-width: 135px; }
  .workspace { padding: 10px; }
  .level-header { grid-template-columns: 1fr auto; }
  .level-fields { grid-column: 1 / -1; }
  .section-heading { flex-direction: column; }
  .matrix-legend { align-items: flex-start; flex-direction: column; gap: 5px; }
  .matrix-legend span:last-child { margin-left: 0; }
  .studio { top: 62px; height: calc(100vh - 62px); }
}
