:root {
  --bg: #f6f3ee;
  --card: #ffffff;
  --ink: #26232b;
  --muted: #77727f;
  --line: #e4dfd7;
  --accent: #ff6b4a;
  --accent-ink: #ffffff;
  --accent-soft: #ffe6df;
  --ok: #2f9e5b;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(40, 30, 20, .08);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
h2 { font-size: 18px; margin: 0 0 8px; }
h3 { font-size: 15px; margin: 0 0 8px; }
input, select, button { font: inherit; color: inherit; }
input, select {
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: #fff; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.hidden { display: none !important; }
.screen { min-height: 100vh; display: flex; flex-direction: column; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; position: relative;
  border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 8px 14px;
  cursor: pointer; white-space: nowrap; user-select: none;
}
.btn:hover { background: #faf8f5; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: rgba(0,0,0,.05); }
.btn.big { padding: 12px 20px; font-size: 16px; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.icon { width: 38px; height: 38px; padding: 0; font-size: 17px; }
.badge {
  position: absolute; top: -3px; right: -3px; background: var(--accent); color: #fff; font-size: 10px;
  min-width: 16px; height: 16px; border-radius: 8px; display: grid; place-items: center; padding: 0 4px;
}
.row { display: flex; gap: 8px; align-items: center; }
.row > input { flex: 1; }
.row.center { justify-content: center; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.logo { font-size: 24px; }
.name-field { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.name-field input { width: 150px; color: var(--ink); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.home-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; padding: 18px; max-width: 1080px; width: 100%; margin: 0 auto; }
.col { display: flex; flex-direction: column; gap: 18px; min-width: 0; }
.upload-card { text-align: center; padding: 34px 18px; border: 2px dashed #e6c9bd; background: #fffaf7; }
.upload-card.drag { border-color: var(--accent); background: var(--accent-soft); }
.upload-card p { color: var(--muted); margin: 0 auto 18px; max-width: 420px; }
.upload-card .hint { margin: 10px 0 0; font-size: 13px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list li .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .sub { color: var(--muted); font-size: 13px; }
.list .empty { color: var(--muted); font-size: 14px; }
.mini-progress { width: 60px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; flex: none; }
.mini-progress div { height: 100%; background: var(--ok); }

.friends-panel h4 { margin: 14px 0 6px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.my-code { display: flex; align-items: center; gap: 8px; background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.my-code code { font-size: 18px; font-weight: 700; letter-spacing: .12em; flex: 1; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #c9c4bd; flex: none; }
.dot.on { background: var(--ok); }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; color: #fff;
  font-weight: 700; font-size: 13px; flex: none; border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line);
}

/* featured */
.featured-wrap { max-width: 1080px; width: 100%; margin: 0 auto; padding: 18px 18px 0; }
.potd { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); gap: 22px; align-items: center; padding: 16px; }
.potd:empty { display: none; }
.potd img { width: 100%; max-height: 360px; object-fit: contain; border-radius: 10px; background: #f1ede6; display: block; }
.potd .kicker { color: var(--accent); font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; }
.potd h1 { font-size: 28px; margin: 4px 0 2px; line-height: 1.15; }
.potd .actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.section-title { margin: 22px 0 10px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; }
.gallery:empty { display: none; }
.pic { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.pic img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; display: block; background: #f1ede6; }
.pic .body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.pic .title { font-weight: 700; }
.pic .by { color: var(--muted); font-size: 13px; }
.pic .actions { display: flex; gap: 6px; margin-top: auto; }
.pic .actions .btn { flex: 1; }
.artist .name { font-weight: 700; font-size: 16px; }
.artist .bio { color: var(--muted); margin: 4px 0 8px; font-size: 14px; }
.links { display: flex; flex-wrap: wrap; gap: 6px; }
.links a { display: inline-block; padding: 4px 10px; border-radius: 999px; background: var(--accent-soft); color: #b2361c; text-decoration: none; font-size: 13px; font-weight: 600; }
.links a:hover { text-decoration: underline; }
.playing { color: var(--ok); font-size: 13px; font-weight: 600; }
.credit { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.linkish { border: 0; background: none; padding: 0; color: var(--accent); font: inherit; font-weight: 600; cursor: pointer; }
.chip { display: inline-block; padding: 0 6px; border-radius: 6px; background: var(--bg); border: 1px solid var(--line); font-size: 11px; font-weight: 600; color: var(--ink); }
#doneArtist .artist { background: var(--bg); border-radius: 10px; padding: 10px; margin: 6px 0; }

/* create */
.create-grid { flex: 1; display: grid; grid-template-columns: 1fr 320px; gap: 18px; padding: 18px; min-height: 0; }
.preview-wrap {
  position: relative; background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  display: grid; place-items: center; overflow: hidden; min-height: 300px;
}
#previewCanvas { max-width: 100%; max-height: calc(100vh - 120px); image-rendering: auto; }
.preview-busy {
  position: absolute; inset: 0; display: flex; flex-direction: column; gap: 12px; align-items: center; justify-content: center;
  background: rgba(255,255,255,.75); font-weight: 600;
}
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 4px solid var(--accent-soft); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.create-options { display: flex; flex-direction: column; gap: 16px; align-self: start; }
.create-options label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
.create-options label b { position: absolute; right: 0; }
.create-options label:has(b) { position: relative; }
input[type=range] { accent-color: var(--accent); padding: 0; border: 0; }
.seg { display: flex; gap: 4px; flex-wrap: wrap; }
.seg .btn.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.stats { color: var(--muted); margin: 0; font-size: 14px; }

/* game */
#game { height: 100vh; height: 100dvh; overflow: hidden; }
.game-bar { padding: 8px 10px; gap: 10px; flex-wrap: nowrap; }
.game-title { flex: 1; min-width: 80px; }
#gameTitle { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.progress { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin-top: 4px; max-width: 260px; }
#progressFill { height: 100%; width: 0; background: var(--ok); transition: width .3s; }
.players { display: flex; }
.players .avatar { margin-left: -6px; }
.players .avatar:first-child { margin-left: 0; }
.bar-actions { display: flex; gap: 4px; align-items: center; }
.game-main { flex: 1; display: flex; min-height: 0; position: relative; }
.viewport {
  flex: 1; position: relative; overflow: hidden; touch-action: none; cursor: crosshair;
  background: #ebe7e0 radial-gradient(#d8d2c8 1px, transparent 1px) 0 0 / 18px 18px;
}
.viewport.panning { cursor: grabbing; }
#boardCanvas { position: absolute; left: 0; top: 0; transform-origin: 0 0; box-shadow: 0 4px 24px rgba(0,0,0,.15); background: #fff; }
#cursors { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.cursor { position: absolute; left: 0; top: 0; transition: transform .08s linear; will-change: transform; }
.cursor svg { display: block; filter: drop-shadow(0 1px 1px rgba(0,0,0,.3)); }
.cursor span { position: absolute; left: 14px; top: 16px; color: #fff; font-size: 12px; font-weight: 600; padding: 1px 6px; border-radius: 6px; white-space: nowrap; }
.peek { position: absolute; right: 12px; top: 12px; max-width: 40%; max-height: 45%; border-radius: 10px; border: 3px solid #fff; box-shadow: var(--shadow); pointer-events: none; }
.zoom { position: absolute; right: 12px; bottom: 12px; display: flex; flex-direction: column; gap: 6px; }
.zoom .btn { box-shadow: var(--shadow); }
.drawer {
  width: 300px; background: var(--card); border-left: 1px solid var(--line); display: flex; flex-direction: column;
  padding: 12px; min-height: 0; overflow: auto;
}
#chatLog { list-style: none; margin: 0 0 8px; padding: 0; flex: 1; overflow-y: auto; font-size: 14px; }
#chatLog li { padding: 3px 0; word-wrap: break-word; }
#chatLog b { margin-right: 4px; }
#chatLog .sys { color: var(--muted); font-style: italic; }

.palette-bar { display: flex; align-items: center; background: var(--card); border-top: 1px solid var(--line); flex: none; min-width: 0; }
.palette { display: flex; gap: 6px; padding: 8px 10px; overflow-x: auto; scrollbar-width: thin; flex: 1; min-width: 0; }
.tools { display: flex; flex-direction: column; gap: 4px; padding: 6px 10px; border-right: 1px solid var(--line); flex: none; align-items: stretch; }
.tools .btn:disabled { opacity: .35; }
.tools .btn.active:disabled { opacity: .8; }
.tools select { padding: 3px 6px; font-size: 13px; border-radius: 8px; }
.tools .lock { font-size: 11px; color: var(--muted); text-align: center; white-space: nowrap; }
.viewport.brushing { cursor: cell; }
.settings-form { display: flex; flex-direction: column; gap: 12px; }
.settings-form label { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; }
.settings-form label:not(.radio) { flex-direction: column; font-weight: 600; }
.settings-form .radio input { margin-top: 3px; }
.swatch {
  flex: none; width: 54px; height: 60px; border-radius: 12px; border: 3px solid transparent; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; position: relative;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.12); padding: 0; background-clip: padding-box;
}
.swatch .num { font-weight: 800; font-size: 17px; }
.swatch .left { font-size: 11px; opacity: .85; }
.swatch.selected { border-color: var(--ink); transform: translateY(-3px); box-shadow: 0 4px 10px rgba(0,0,0,.2); }
.swatch.done { opacity: .45; }
.swatch.done .left::before { content: '✓ '; }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.shake { animation: shake .25s ease 2; }

.overlay { position: fixed; inset: 0; background: rgba(30, 25, 35, .55); display: grid; place-items: center; z-index: 50; padding: 16px; }
.done-card { text-align: center; max-width: 520px; width: 100%; max-height: 100%; overflow: auto; }
#doneCanvas { max-width: 100%; max-height: 50vh; border-radius: 8px; margin: 8px 0; }
#scoreList { text-align: left; max-width: 300px; margin: 8px auto 16px; padding-left: 24px; }
#scoreList li { padding: 2px 0; }

#toasts { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 60; align-items: center; pointer-events: none; }
.toast {
  background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow);
  display: flex; gap: 10px; align-items: center; pointer-events: auto; max-width: 90vw; animation: pop .2s ease;
}
.toast .btn { padding: 4px 10px; }
@keyframes pop { from { transform: translateY(10px); opacity: 0; } }

@media (max-width: 820px) {
  .home-grid, .create-grid { grid-template-columns: 1fr; padding: 12px; }
  .featured-wrap { padding: 12px 12px 0; }
  .potd { grid-template-columns: 1fr; }
  .potd h1 { font-size: 22px; }
  .gallery { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  #previewCanvas { max-height: 55vh; }
  .drawer { position: absolute; right: 0; top: 0; bottom: 0; width: min(320px, 90vw); z-index: 5; box-shadow: var(--shadow); }
  .players { display: none; }
  .tools { padding: 6px; }
  .tools .btn.small { padding: 4px 6px; }
  #copyLinkBtn { padding: 8px 10px; }
  .name-field input { width: 110px; }
}

/* storage */
.storage { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.storage .bar { height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; margin: 6px 0; }
.storage .bar div { height: 100%; background: var(--ok); }
.storage .bar.full div { background: var(--accent); }
.storage form { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.storage textarea { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font: inherit; resize: vertical; }

/* admin */
.admin { max-width: 1080px; width: 100%; margin: 0 auto; padding: 18px; }
.admin h2 { margin: 18px 0 8px; }
.admin .card { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin-table th { text-align: left; color: var(--muted); font-weight: 600; padding: 6px 8px; }
.admin-table td { padding: 8px; border-top: 1px solid var(--line); vertical-align: middle; }
.row > .mb-input { flex: 0 0 90px; width: 90px; }
.admin-nav { margin-bottom: 4px; }
.admin-nav .btn { text-decoration: none; }
.admin-table .thumb { width: 72px; height: 54px; object-fit: cover; border-radius: 8px; display: block; background: var(--line); }
.admin-table .actions { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.admin-table tr.editing td { background: var(--accent-soft); }
.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 99px; background: var(--accent-soft); color: #b5381c; font-weight: 600; }
.tag.bad { background: #fde2e2; color: #a12020; }
.feat-form { margin-top: 12px; display: flex; flex-direction: column; gap: 12px; }
.feat-grid { display: grid; grid-template-columns: 220px 1fr; gap: 16px; }
.feat-image { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.feat-image img { width: 100%; max-height: 220px; object-fit: contain; border-radius: 10px; background: var(--bg); }
.feat-fields { display: flex; flex-direction: column; gap: 10px; }
.feat-fields label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.feat-fields textarea { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font: inherit; resize: vertical; }
.feat-fields .row.wrap { flex-wrap: wrap; align-items: flex-end; gap: 12px; }
.feat-fields .row.wrap .mb-input { width: 90px; }
.link-row { margin-bottom: 6px; }
.link-row input:first-child { flex: 0 0 130px; width: 130px; }
@media (max-width: 640px) { .feat-grid { grid-template-columns: 1fr; } }
