/* ---------- Tokens ---------- */
:root {
  --bg: #f5efe4;
  --bg-2: #ece3d3;
  --surface: #fffaf2;
  --ink: #2a2320;
  --ink-2: #6b5f57;
  --line: #ddd0bc;
  --accent: #e0533d;
  --accent-ink: #ffffff;
  --focus: #2f6fdb;
  --shadow: 0 1px 0 rgba(60, 40, 20, .08), 0 6px 18px rgba(60, 40, 20, .08);
  --radius: 16px;

  --red: #e04a3c; --blue: #2f74d8; --green: #2f9e5b; --yellow: #f1b62b;
  --sea: #cfe4f2; --sea-line: #a9cbe3; --ship: #59636f;

  --t-ttt: #f6d3c9; --t-bs: #cfe2f3; --t-ludo: #d6ecd9; --t-uno: #fbe3b3;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17191c; --bg-2: #1f2226; --surface: #23272c; --ink: #f1ece4; --ink-2: #a8a197;
    --line: #363b42; --accent: #f06a52; --shadow: 0 1px 0 rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
    --sea: #1d3447; --sea-line: #2c4b63; --ship: #9aa5b1;
    --t-ttt: #43302b; --t-bs: #253747; --t-ludo: #253b2c; --t-uno: #463b22;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17191c; --bg-2: #1f2226; --surface: #23272c; --ink: #f1ece4; --ink-2: #a8a197;
  --line: #363b42; --accent: #f06a52; --shadow: 0 1px 0 rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
  --sea: #1d3447; --sea-line: #2c4b63; --ship: #9aa5b1;
  --t-ttt: #43302b; --t-bs: #253747; --t-ludo: #253b2c; --t-uno: #463b22;
  color-scheme: dark;
}

/* ---------- Bas ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 Nunito, system-ui, sans-serif;
  min-height: 100vh;
}
h1, h2, h3, .brand, .big-code, .tile-name { font-family: Fredoka, Nunito, sans-serif; letter-spacing: .01em; }
h1 { font-size: clamp(1.6rem, 4vw, 2.4rem); margin: .2em 0; line-height: 1.15; }
h2 { font-size: 1.25rem; margin: 1.2em 0 .5em; }
h3 { font-size: 1.05rem; margin: 0 0 .5em; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.muted { color: var(--ink-2); }
.small { font-size: .85rem; }
.center { text-align: center; justify-content: center; }
.inline { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }

main { max-width: 1100px; margin: 0 auto; padding: 0 16px 48px; }

/* ---------- Toppbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 10px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
}
.brand { font-size: 1.3rem; font-weight: 700; color: var(--ink); text-decoration: none; }
#topbar-extra { display: flex; gap: .5rem; align-items: center; }
.room-pill { font-weight: 800; font-size: .85rem; padding: .25rem .6rem; border-radius: 99px; background: var(--bg-2); border: 1px solid var(--line); }

/* ---------- Knappar och fält ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .65rem 1.1rem; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 800; cursor: pointer;
  box-shadow: 0 2px 0 var(--line); transition: transform .08s, box-shadow .08s;
  min-height: 44px;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); }
.btn:active:not(:disabled) { transform: translateY(1px); box-shadow: 0 0 0 var(--line); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; box-shadow: 0 3px 0 color-mix(in srgb, var(--accent) 60%, black); }
.btn.small { padding: .35rem .8rem; min-height: 34px; font-size: .9rem; }
.btn.ghost { background: transparent; box-shadow: none; }
.icon-btn { margin-left: auto; border: 0; background: none; cursor: pointer; font-size: 1rem; padding: .3rem .5rem; border-radius: 8px; color: var(--ink-2); }
.icon-btn:hover { background: var(--bg-2); }
.link-back { border: 0; background: none; color: var(--ink-2); cursor: pointer; padding: .5rem 0; font-weight: 700; }
.link-back:hover { color: var(--ink); }

.field { display: flex; flex-direction: column; gap: .3rem; font-weight: 700; font-size: .9rem; }
.field input {
  font: inherit; font-weight: 600; padding: .65rem .8rem; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink); min-height: 44px;
}
.code-input { width: 7.5rem; text-transform: uppercase; letter-spacing: .25em; font-weight: 800 !important; text-align: center; }

/* ---------- Startsida ---------- */
.hero { padding: 32px 0 20px; }
.lead { font-size: 1.1rem; color: var(--ink-2); max-width: 36rem; margin: .2rem 0 1.4rem; }
.hero-row { display: flex; gap: 1.2rem; flex-wrap: wrap; align-items: flex-end; }
.name-field input { width: 14rem; max-width: 100%; }

.game-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 16px; margin-top: 16px; }
.game-tile {
  display: flex; flex-direction: column; align-items: flex-start; text-align: left; gap: .3rem;
  padding: 20px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--surface); box-shadow: var(--shadow); cursor: pointer;
  transition: transform .15s, box-shadow .15s; position: relative; overflow: hidden;
}
.game-tile::before { content: ''; position: absolute; inset: 0 0 auto 0; height: 92px; background: var(--tile); z-index: 0; }
.game-tile > * { position: relative; }
.game-tile:hover { transform: translateY(-3px) rotate(-.4deg); }
.tile-icon { font-size: 3rem; line-height: 1; margin: 6px 0 14px; filter: drop-shadow(0 3px 0 rgba(0,0,0,.12)); }
.tile-name { font-size: 1.35rem; font-weight: 700; }
.tile-desc { color: var(--ink-2); font-size: .95rem; flex: 1; }
.tile-players { font-size: .8rem; font-weight: 800; padding: .15rem .55rem; border-radius: 99px; background: var(--bg-2); margin-top: .4rem; }
.tile-tictactoe { --tile: var(--t-ttt); } .tile-battleship { --tile: var(--t-bs); }
.tile-ludo { --tile: var(--t-ludo); } .tile-uno { --tile: var(--t-uno); }

/* ---------- Setup ---------- */
.setup { padding-top: 16px; max-width: 760px; }
.setup-head { display: flex; gap: 1rem; align-items: center; padding: 18px 20px; border-radius: var(--radius); background: var(--tile); margin: 8px 0 20px; }
.setup-head .tile-icon { margin: 0; }
.setup-head p { margin: 0; color: var(--ink-2); }
.mode-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; margin-top: 20px; }
.mode-card { display: flex; flex-direction: column; gap: .8rem; align-items: flex-start; padding: 20px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.mode-card h2 { margin: 0; }
.mode-card p { margin: 0; color: var(--ink-2); flex: 1; }
.seg { display: inline-flex; background: var(--bg-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: none; padding: .45rem .8rem; border-radius: 9px; cursor: pointer; font-weight: 700; color: var(--ink-2); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.12); }

/* ---------- Lobby ---------- */
.lobby { max-width: 560px; margin: 0 auto; padding-top: 16px; text-align: center; }
.lobby .link-back { display: block; text-align: left; }
.eyebrow { font-weight: 800; color: var(--ink-2); margin: .8rem 0 0; }
.big-code { font-size: clamp(3rem, 14vw, 5rem); font-weight: 700; letter-spacing: .18em; margin: 0 0 .6rem; padding-left: .18em; }
.seats { list-style: none; padding: 0; margin: 0 0 1rem; display: flex; flex-direction: column; gap: .5rem; text-align: left; }
.seat { display: flex; align-items: center; gap: .6rem; padding: .6rem .8rem; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.seat.offline { opacity: .6; }
.seat-avatar { font-size: 1.2rem; }
.seat-name { font-weight: 700; }
.badge { font-size: .72rem; font-weight: 800; padding: .1rem .5rem; border-radius: 99px; background: var(--accent); color: #fff; white-space: nowrap; }
.badge.muted { background: var(--bg-2); color: var(--ink-2); }

/* ---------- Spelvy ---------- */
.game { padding-top: 12px; }
.game-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .8rem; }
.game-head h1 { font-size: 1.6rem; }
.seats.compact { flex-direction: row; flex-wrap: wrap; margin: 0; }
.seats.compact .seat { padding: .35rem .65rem; font-size: .9rem; transition: box-shadow .2s, border-color .2s; }
.seats.compact .seat.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.status { margin: 14px 0; padding: .75rem 1rem; border-radius: 12px; background: var(--bg-2); font-weight: 700; min-height: 3rem; }
.status.mine { background: color-mix(in srgb, var(--accent) 16%, var(--surface)); }
.status.done { background: var(--accent); color: #fff; font-family: Fredoka, sans-serif; font-size: 1.3rem; text-align: center; }
.over-bar { display: flex; gap: .8rem; justify-content: center; flex-wrap: wrap; margin-top: 20px; }
.board-wrap { display: flex; flex-direction: column; align-items: center; gap: 16px; }

/* ---------- Tre i rad ---------- */
.ttt { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: min(360px, 100%); aspect-ratio: 1; }
.ttt-cell {
  border: 0; border-radius: 16px; background: var(--surface); box-shadow: var(--shadow);
  font-size: clamp(2.5rem, 12vw, 4rem); font-weight: 700; cursor: pointer; line-height: 1;
  display: grid; place-items: center; transition: transform .1s, background .2s;
}
.ttt-cell:disabled { cursor: default; }
.ttt-cell:not(:disabled):hover { background: var(--bg-2); transform: scale(1.03); }
.ttt-cell.m0, .ttt-mark.m0 { color: var(--accent); }
.ttt-cell.m1, .ttt-mark.m1 { color: var(--blue); }
.ttt-cell.win { background: color-mix(in srgb, var(--yellow) 45%, var(--surface)); }
.ttt-mark { font-weight: 800; }
.ttt-cell.m0, .ttt-cell.m1 { animation: pop .2s ease-out; }
@keyframes pop { from { transform: scale(.6); } to { transform: scale(1); } }

/* ---------- Sänka skepp ---------- */
.bs-boards { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 24px; width: 100%; }
.bs-boards.single { max-width: 460px; }
.bs-board { background: var(--surface); padding: 14px; border-radius: var(--radius); border: 1px solid var(--line); box-shadow: var(--shadow); }
.bs-board.focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent), var(--shadow); }
.bs-board h3 { text-align: center; }
.bs-grid { display: grid; grid-template-columns: 1.2em repeat(10, 1fr); gap: 2px; width: 100%; }
.bs-label { font-size: .7rem; font-weight: 800; color: var(--ink-2); display: grid; place-items: center; }
.bs-cell {
  aspect-ratio: 1; background: var(--sea); border: 0; border-radius: 3px; padding: 0;
  display: grid; place-items: center; font-size: clamp(.7rem, 2.6vw, 1rem); font-weight: 800; color: var(--ink);
}
.bs-cell.ship { background: var(--ship); }
.bs-cell.ship.h.start { border-radius: 40% 3px 3px 40%; } .bs-cell.ship.h.end { border-radius: 3px 40% 40% 3px; }
.bs-cell.ship.v.start { border-radius: 40% 40% 3px 3px; } .bs-cell.ship.v.end { border-radius: 3px 3px 40% 40%; }
.bs-cell.miss { color: var(--ink-2); }
.bs-cell.hit { background: var(--red); color: #fff; }
.bs-cell.ship.hit { background: var(--red); }
.bs-cell.sunk { background: #7a1f17; color: #fbd; }
.bs-cell.last { outline: 2px solid var(--yellow); outline-offset: -2px; }
.bs-grid.target button.bs-cell { cursor: crosshair; }
.bs-grid.target button.bs-cell:hover { background: color-mix(in srgb, var(--accent) 35%, var(--sea)); }
.bs-board .inline { margin-top: 12px; }

/* ---------- Fia med knuff ---------- */
.ludo-layout { display: flex; flex-wrap: wrap; gap: 20px; justify-content: center; align-items: flex-start; width: 100%; }
.ludo-board {
  display: grid; grid-template-columns: repeat(11, minmax(0, 1fr)); grid-template-rows: repeat(11, minmax(0, 1fr)); gap: 2px;
  width: min(560px, 100%); aspect-ratio: 1; padding: 8px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.lc { position: relative; display: grid; place-items: center; border-radius: 50%; min-width: 0; min-height: 0; overflow: visible; }
.lc.empty { background: none; }
.lc.yard-bg { border-radius: 4px; }
.lc.yard-bg.c0 { background: color-mix(in srgb, var(--red) 22%, transparent); }
.lc.yard-bg.c1 { background: color-mix(in srgb, var(--blue) 22%, transparent); }
.lc.yard-bg.c2 { background: color-mix(in srgb, var(--green) 22%, transparent); }
.lc.yard-bg.c3 { background: color-mix(in srgb, var(--yellow) 30%, transparent); }
.lc.yard-spot { border-radius: 50%; box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--ink) 15%, transparent); background-clip: content-box; }
.lc.track { background: var(--bg-2); border: 1px solid var(--line); }
.lc.home.c0, .lc.start.c0 { background: color-mix(in srgb, var(--red) 55%, var(--surface)); }
.lc.home.c1, .lc.start.c1 { background: color-mix(in srgb, var(--blue) 55%, var(--surface)); }
.lc.home.c2, .lc.start.c2 { background: color-mix(in srgb, var(--green) 55%, var(--surface)); }
.lc.home.c3, .lc.start.c3 { background: color-mix(in srgb, var(--yellow) 65%, var(--surface)); }
.lc.goal { border-radius: 8px; background: conic-gradient(var(--blue) 0 25%, var(--green) 0 50%, var(--yellow) 0 75%, var(--red) 0); opacity: .9; }
.pieces { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; width: 100%; height: 100%; gap: 1px; }
.piece {
  display: block; width: 78%; aspect-ratio: 1; border-radius: 50%; padding: 0;
  border: 2px solid rgba(0, 0, 0, .35); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .25), 0 2px 3px rgba(0, 0, 0, .25);
}
.pieces.n2 .piece, .pieces.n3 .piece, .pieces.n4 .piece { width: 46%; }
.goal-pieces .piece { width: 28% !important; border-width: 1px; }
.piece.c0 { background: var(--red); } .piece.c1 { background: var(--blue); }
.piece.c2 { background: var(--green); } .piece.c3 { background: var(--yellow); }
.piece.movable { cursor: pointer; animation: bob .8s ease-in-out infinite alternate; outline: 3px solid var(--ink); outline-offset: 1px; }
@keyframes bob { from { transform: translateY(0); } to { transform: translateY(-4px); } }
.ludo-side { display: flex; flex-direction: column; align-items: center; gap: 12px; width: 220px; max-width: 100%; text-align: center; }
.dice {
  width: 96px; height: 96px; display: grid; place-items: center; font-size: 84px; line-height: 1;
  background: var(--surface); border-radius: 20px; border: 1px solid var(--line); box-shadow: var(--shadow);
}
.dice.c0 { color: var(--red); } .dice.c1 { color: var(--blue); } .dice.c2 { color: var(--green); } .dice.c3 { color: var(--yellow); }
.badge.ludo-c0 { background: var(--red); } .badge.ludo-c1 { background: var(--blue); }
.badge.ludo-c2 { background: var(--green); } .badge.ludo-c3 { background: var(--yellow); color: #3a2a00; }

/* ---------- Uno ---------- */
.board-uno { gap: 22px; }
.uno-opps { display: flex; gap: 18px; justify-content: center; flex-wrap: wrap; }
.uno-opp { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 14px; border-radius: 14px; border: 2px solid transparent; }
.uno-opp.active { border-color: var(--accent); background: var(--surface); }
.uno-backs { display: flex; }
.uno-backs .uno-back { margin-left: -14px; } .uno-backs .uno-back:first-child { margin-left: 0; }
.uno-back {
  display: inline-grid; place-items: center; width: 28px; height: 42px; border-radius: 5px;
  background: #1d1d1f; border: 2px solid #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  color: var(--yellow); font-family: Fredoka, sans-serif; font-weight: 700; font-size: .8rem;
  background-image: radial-gradient(ellipse at center, var(--red) 0 45%, transparent 46%);
}
.uno-back.big { width: 78px; height: 116px; border-radius: 10px; border-width: 4px; font-size: 1.2rem; transform: rotate(-4deg); }
.uno-opp-name { font-weight: 700; font-size: .9rem; }
.uno-table { display: flex; gap: 28px; align-items: center; justify-content: center; flex-wrap: wrap; padding: 22px; border-radius: 24px; background: color-mix(in srgb, var(--green) 25%, var(--surface)); width: min(560px, 100%); }
.uno-deck { display: flex; flex-direction: column; align-items: center; gap: 6px; border: 0; background: none; cursor: pointer; padding: 0; }
.uno-deck:disabled { cursor: default; opacity: .75; }
.uno-deck:not(:disabled):hover .uno-back { transform: rotate(-4deg) translateY(-4px); }
.uno-info { display: flex; flex-direction: column; gap: 8px; align-items: center; }
.uno-color { padding: .3rem .8rem; border-radius: 99px; color: #fff; font-weight: 800; }
.uno-dir { font-weight: 800; color: var(--ink-2); }
.uno-hand { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; padding: 8px; max-width: 100%; }
.uno-card {
  position: relative; width: 78px; height: 116px; border-radius: 10px; border: 4px solid #fff; padding: 0;
  display: grid; place-items: center; box-shadow: 0 2px 6px rgba(0, 0, 0, .25); color: #fff;
  font-family: Fredoka, sans-serif; font-weight: 700; transition: transform .12s;
}
button.uno-card { cursor: pointer; }
.uno-card.playable { transform: translateY(-8px); box-shadow: 0 0 0 3px var(--ink), 0 6px 12px rgba(0, 0, 0, .3); }
.uno-card.playable:hover { transform: translateY(-14px); }
.uno-hand .uno-card:not(.playable) { filter: saturate(.75) brightness(.92); }
.uno-oval {
  width: 80%; height: 66%; border-radius: 50%; background: #fff; transform: rotate(-25deg);
  display: grid; place-items: center; font-size: 1.9rem; color: var(--card);
}
.uno-oval { text-shadow: none; }
.uno-corner { position: absolute; top: 3px; left: 6px; font-size: .9rem; text-shadow: 0 1px 0 rgba(0,0,0,.4); }
.u-r { --card: var(--red); background-color: var(--red); }
.u-y { --card: #d39a0c; background-color: var(--yellow); }
.u-g { --card: var(--green); background-color: var(--green); }
.u-b { --card: var(--blue); background-color: var(--blue); }
.u-w { --card: #1d1d1f; background: conic-gradient(var(--red) 0 25%, var(--blue) 0 50%, var(--yellow) 0 75%, var(--green) 0); }
.uno-card.u-w { background: #1d1d1f; }
.uno-card.u-w .uno-oval { background: conic-gradient(var(--red) 0 25%, var(--blue) 0 50%, var(--yellow) 0 75%, var(--green) 0); color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,.6); }
.uno-color.u-y { color: #3a2a00; }
.badge.uno-alert { background: var(--yellow); color: #3a2a00; animation: pop .3s; }

.modal-back { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); padding: 22px; border-radius: var(--radius); box-shadow: var(--shadow); text-align: center; width: min(360px, 100%); }
.color-pick { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.color-pick button { border: 0; border-radius: 12px; padding: 1.2rem; font-weight: 800; color: #fff; cursor: pointer; font-size: 1.05rem; }
.color-pick button.u-y { color: #3a2a00; }
.color-pick button:hover { transform: scale(1.04); }

/* ---------- Notiser ---------- */
#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100; width: min(420px, calc(100% - 32px)); }
.toast { background: var(--ink); color: var(--bg); padding: .7rem 1rem; border-radius: 12px; font-weight: 700; box-shadow: var(--shadow); transition: opacity .4s, transform .4s; }
.toast.warn { background: var(--accent); color: #fff; }
.toast.out { opacity: 0; transform: translateY(8px); }

/* ---------- Mobil ---------- */
@media (max-width: 560px) {
  .uno-card { width: 58px; height: 88px; border-width: 3px; }
  .uno-oval { font-size: 1.4rem; }
  .uno-back.big { width: 60px; height: 90px; }
  .uno-table { gap: 16px; padding: 16px; }
  .game-head h1 { font-size: 1.3rem; }
  .bs-boards { grid-template-columns: 1fr; }
  .ludo-side { order: -1; flex-direction: row; flex-wrap: wrap; justify-content: center; width: 100%; }
  .ludo-side p { display: none; }
  .dice { width: 64px; height: 64px; font-size: 56px; border-radius: 14px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
