/* ---------- Färger: ljust tema (marinblått) ---------- */
:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --panel: rgba(255, 255, 255, 0.96);
  --ink: #0e1a2f;
  --ink-2: #45526b;
  --muted: #7b869c;
  --line: #dfe4ec;
  --hover: rgba(14, 26, 47, 0.06);
  --input: #ffffff;
  --accent: #1e3a6d;
  --accent-hover: #28498a;
  --accent-ink: #ffffff;
  --accent-text: #1e3a6d;
  --accent-soft: #e4eaf4;
  --swash: #7fa3dc;
  --stage: #e5e8ee;
  --stage-dot: rgba(14, 26, 47, 0.09);
  --danger: #d93f45;
  --ok: #2f9e69;
  --tip: #0e1a2f;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(14, 26, 47, 0.06), 0 6px 22px rgba(14, 26, 47, 0.1);
  --shadow-lg: 0 18px 50px rgba(14, 26, 47, 0.22);
  --font: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: light;
}

/* ---------- Mörkt tema ---------- */
:root[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #141d30;
  --panel: rgba(20, 29, 48, 0.96);
  --ink: #e6ebf4;
  --ink-2: #b0bacd;
  --muted: #7d89a3;
  --line: #26324c;
  --hover: rgba(255, 255, 255, 0.07);
  --input: #0f1728;
  --accent: #34589a;
  --accent-hover: #3f67b0;
  --accent-ink: #ffffff;
  --accent-text: #8fb0ea;
  --accent-soft: rgba(143, 176, 234, 0.14);
  --swash: #4a6fb0;
  --stage: #070c17;
  --stage-dot: rgba(255, 255, 255, 0.06);
  --tip: #e6ebf4;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 26px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 18px 50px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #0b1220; --surface: #141d30; --panel: rgba(20, 29, 48, 0.96); --ink: #e6ebf4; --ink-2: #b0bacd;
    --muted: #7d89a3; --line: #26324c; --hover: rgba(255, 255, 255, 0.07); --input: #0f1728; --accent: #34589a;
    --accent-hover: #3f67b0; --accent-text: #8fb0ea; --accent-soft: rgba(143, 176, 234, 0.14); --swash: #4a6fb0;
    --stage: #070c17; --stage-dot: rgba(255, 255, 255, 0.06); --tip: #e6ebf4; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg.sm, .sm svg { width: 17px; height: 17px; }
:focus-visible { outline: 2.5px solid var(--accent-text); outline-offset: 2px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); font-weight: 600; margin: 14px 0 0; }
.row { display: flex; align-items: center; gap: 6px; }
.row.end { justify-content: flex-end; }
.row-between { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.row-between > span:first-child { white-space: nowrap; }

/* ---------- Knappar ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: 0; border-radius: 10px; padding: 11px 18px; font-weight: 700; font-size: 15px;
  transition: transform 0.08s, background 0.15s; white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--hover); }
.btn.big { padding: 14px 22px; font-size: 16px; width: 100%; }
.btn.small { padding: 8px 12px; font-size: 14px; border-radius: 9px; }
.btn.danger { color: var(--danger); }
.btn.danger-bg { background: var(--danger); color: #fff; }
.btn:disabled { opacity: 0.55; pointer-events: none; }

.icon-btn {
  width: 40px; height: 40px; display: inline-grid; place-items: center; flex: none;
  border: 0; border-radius: 10px; background: transparent; color: var(--ink-2);
  transition: background 0.12s, color 0.12s;
}
.icon-btn.sm { width: 32px; height: 32px; border-radius: 8px; }
.icon-btn:hover { background: var(--hover); color: var(--ink); }
.icon-btn:disabled { opacity: 0.3; pointer-events: none; }

.field { display: block; margin: 0 0 14px; }
.field > span { display: block; font-size: 13px; font-weight: 700; color: var(--ink-2); margin: 0 0 6px; }
.field input, .join-row input {
  width: 100%; border: 1.5px solid var(--line); background: var(--input); border-radius: 10px;
  padding: 12px 14px; font-size: 16px; outline: none; transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:focus, .join-row input:focus { border-color: var(--accent-text); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Startsida ---------- */
#home { min-height: 100dvh; display: flex; flex-direction: column; overflow-x: hidden; }
.home-top {
  display: flex; justify-content: flex-end; align-items: center; gap: 6px;
  padding: calc(14px + var(--safe-t)) 16px 0; min-height: 56px;
}
#accountBox { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.acc-name { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; }
.acc-name .av { width: 28px; height: 28px; font-size: 12px; }

.hero {
  flex: 1; width: 100%; max-width: 760px; margin: 0 auto; padding: 28px 16px 32px;
  display: flex; flex-direction: column; align-items: center; text-align: center;
}
.hero-mark {
  width: 64px; height: 64px; border-radius: 18px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); margin-bottom: 20px;
}
.hero-mark svg { width: 40px; height: 40px; }
.hero h1 { font-size: clamp(48px, 11vw, 84px); line-height: 1; letter-spacing: -0.045em; margin: 0 0 18px; font-weight: 850; color: var(--ink); }
.hero h1 .paint { position: relative; color: var(--accent-text); }
.swash { position: absolute; left: 2%; bottom: -0.14em; width: 96%; height: 0.24em; overflow: visible; }
.swash path { stroke: var(--swash); stroke-width: 7; fill: none; stroke-linecap: round; opacity: 0.8; }
.lead { font-size: 19px; color: var(--ink-2); margin: 0 0 34px; max-width: 30ch; line-height: 1.45; text-wrap: balance; }
.name-field { width: 100%; max-width: 320px; margin-bottom: 20px; }
.name-field input { text-align: center; font-weight: 600; }
.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; width: 100%; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 24px 22px 22px;
  display: flex; flex-direction: column; align-items: center; text-align: center; box-shadow: 0 1px 2px rgba(14, 26, 47, 0.04);
}
.card h2 { margin: 0 0 6px; font-size: 21px; letter-spacing: -0.02em; }
.card p { margin: 0 0 18px; color: var(--ink-2); font-size: 15px; flex: 1; max-width: 26ch; text-wrap: balance; }
.join-row { display: flex; gap: 8px; width: 100%; }
.join-row input { font-family: ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 0.12em; min-width: 0; text-align: center; }
.home-foot { text-align: center; color: var(--muted); font-size: 13px; padding: 18px 16px calc(18px + var(--safe-b)); }

/* Havet: startsidan ligger "under ytan" i både ljust och mörkt läge */
#home { position: relative; color: #fff; background: #0d2c5a; }
#ocean { position: fixed; inset: 0; width: 100%; height: 100%; display: block; z-index: 0; }
#home > :not(#ocean) { position: relative; z-index: 1; }
#home .hero-mark {
  background: rgba(255, 255, 255, 0.14); border: 1px solid rgba(255, 255, 255, 0.3); color: #fff;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); animation: bob 6s ease-in-out infinite;
}
@keyframes bob { 50% { transform: translateY(-6px) rotate(-3deg); } }
#home h1 { color: #fff; text-shadow: 0 2px 30px rgba(0, 20, 60, 0.35); }
#home h1 .paint { color: #a9dcff; }
#home .swash path { stroke: #a9dcff; opacity: 0.75; }
#home .lead { color: rgba(255, 255, 255, 0.88); }
#home .field > span { color: rgba(255, 255, 255, 0.82); }
#home .field input, #home .join-row input {
  background: rgba(255, 255, 255, 0.12); border-color: rgba(255, 255, 255, 0.28); color: #fff;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
#home input::placeholder { color: rgba(255, 255, 255, 0.6); }
#home .field input:focus, #home .join-row input:focus { border-color: #a9dcff; box-shadow: 0 0 0 3px rgba(169, 220, 255, 0.25); }
#home .card {
  background: rgba(255, 255, 255, 0.1); border-color: rgba(255, 255, 255, 0.22); color: #fff;
  backdrop-filter: blur(16px) saturate(1.2); -webkit-backdrop-filter: blur(16px) saturate(1.2);
  box-shadow: 0 12px 40px rgba(0, 15, 40, 0.25);
}
#home .card p { color: rgba(255, 255, 255, 0.78); }
#home .btn.primary { background: #fff; color: #0e2a55; }
#home .btn.primary:hover { background: #e3f2ff; }
#home .btn.ghost, #home .icon-btn { color: rgba(255, 255, 255, 0.92); }
#home .btn.ghost:hover, #home .icon-btn:hover { background: rgba(255, 255, 255, 0.14); color: #fff; }
#home .home-foot { color: rgba(255, 255, 255, 0.65); }
#home .error { color: #ffd0d0; background: rgba(110, 10, 20, 0.35); padding: 8px 14px; border-radius: 9px; }
#home .hero > * { animation: rise 0.9s cubic-bezier(0.2, 0.7, 0.2, 1) both; }
#home .hero > :nth-child(2) { animation-delay: 0.08s; }
#home .hero > :nth-child(3) { animation-delay: 0.16s; }
#home .hero > :nth-child(4) { animation-delay: 0.24s; }
#home .hero > :nth-child(5) { animation-delay: 0.32s; }
#home .hero > .hero-mark { animation: rise 0.9s cubic-bezier(0.2, 0.7, 0.2, 1) both, bob 6s ease-in-out 1s infinite; }
@keyframes rise { from { opacity: 0; transform: translateY(18px); } }

@media (max-width: 640px) {
  .cards { grid-template-columns: 1fr; }
  .hero { padding-top: 12px; }
  .hero-mark { width: 56px; height: 56px; border-radius: 16px; }
  .hero-mark svg { width: 34px; height: 34px; }
  .lead { font-size: 17px; margin-bottom: 26px; }
}

/* ---------- Tavla ---------- */
#boardScreen { position: fixed; inset: 0; overflow: hidden; background: var(--stage); }
#stage {
  position: absolute; inset: 0; overflow: hidden; touch-action: none;
  background-color: #fff;
  background-image: radial-gradient(rgba(14, 26, 47, 0.13) 1px, transparent 1.2px);
  background-size: 24px 24px;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
#stage.dark-bg { background-image: radial-gradient(rgba(255, 255, 255, 0.16) 1px, transparent 1.2px); }
#stage.pan { cursor: grab; }
#stage.panning { cursor: grabbing; }
#stage.draw { cursor: crosshair; }
#stage.text { cursor: text; }
#wrap { position: absolute; left: 0; top: 0; width: 0; height: 0; transform-origin: 0 0; }
#tiles { position: absolute; left: 0; top: 0; }
.tile { position: absolute; width: 512px; height: 512px; display: block; pointer-events: none; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 2; }
#cursors { position: absolute; inset: 0; pointer-events: none; overflow: visible; }
.cursor { position: absolute; left: 0; top: 0; pointer-events: none; will-change: transform; transition: transform 0.08s linear; }
.cursor-inner { transform: scale(var(--inv, 1)); transform-origin: 0 0; display: flex; align-items: flex-start; }
.cursor svg { width: 22px; height: 22px; stroke: #fff; stroke-width: 1.5; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.25)); }
.cursor span {
  margin: 16px 0 0 -4px; padding: 3px 8px; border-radius: 7px; color: #fff; font-size: 12px; font-weight: 700;
  white-space: nowrap; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}
#brushCursor {
  position: absolute; left: 0; top: 0; border-radius: 50%; pointer-events: none;
  border: 1px solid rgba(0, 0, 0, 0.7); box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.8);
}
#textInput {
  position: absolute; margin: 0; padding: 0; border: 0; outline: 2px dashed #7fa3dc; outline-offset: 4px;
  background: transparent; resize: none; overflow: hidden; white-space: pre; line-height: 1.2;
  font-weight: 600; font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; min-width: 1ch;
}

/* Handtag för att ändra tavlans storlek */
.handle {
  position: absolute; left: 0; top: 0; width: 14px; height: 14px; margin: -7px 0 0 -7px; z-index: 5;
  background: var(--surface); border: 2px solid var(--accent-text); border-radius: 4px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
  touch-action: none;
}
.handle::after { content: ""; position: absolute; inset: -12px; }
.handle[data-dir="r"] { cursor: ew-resize; }
.handle[data-dir="b"] { cursor: ns-resize; }
.handle[data-dir="br"] { cursor: nwse-resize; }
.handle:hover { background: var(--accent-soft); }
#resizeGhost {
  position: absolute; left: 0; top: 0; z-index: 4; pointer-events: none;
  border: 2px dashed var(--accent-text); background: rgba(30, 58, 109, 0.06);
}
#resizeGhost span {
  position: absolute; right: 6px; bottom: 6px; background: var(--tip); color: var(--bg); font-size: 12px; font-weight: 700;
  padding: 4px 8px; border-radius: 7px; font-variant-numeric: tabular-nums; white-space: nowrap;
}

.panel {
  position: absolute; z-index: 10; background: var(--panel); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 5px;
}
#topBar { top: calc(10px + var(--safe-t)); left: calc(10px + var(--safe-l)); max-width: calc(100vw - 20px); }
.chip {
  display: inline-flex; align-items: center; gap: 8px; border: 0; background: var(--hover);
  border-radius: 10px; height: 40px; padding: 0 12px; font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-weight: 700; font-size: 16px; letter-spacing: 0.12em; color: var(--ink); flex: none;
}
.chip:hover { filter: brightness(0.97); }
.chip svg { color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px rgba(47, 158, 105, 0.2); }
.dot.off { background: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }
.lock-badge { display: grid; place-items: center; width: 28px; color: var(--accent-text); }

#users { display: flex; align-items: center; padding: 0 2px 0 6px; }
.av {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: #fff;
  font-weight: 800; font-size: 13px; border: 2.5px solid var(--surface); margin-left: -8px; position: relative;
  background: var(--c, #888); cursor: pointer; padding: 0; text-transform: uppercase; flex: none;
}
.av:first-child { margin-left: 0; }
.av.me::after {
  content: ""; position: absolute; right: -3px; bottom: -3px; width: 11px; height: 11px; border-radius: 50%;
  background: var(--ok); border: 2px solid var(--surface);
}
.av.more { background: var(--line); color: var(--ink-2); }

/* Verktyg */
#tools {
  left: calc(10px + var(--safe-l)); top: calc(66px + var(--safe-t));
  display: flex; flex-direction: column; gap: 2px;
  max-height: calc(100dvh - 66px - 76px - var(--safe-t) - var(--safe-b)); overflow-y: auto; scrollbar-width: none;
}
#tools::-webkit-scrollbar { display: none; }
.tool-sec { display: contents; }
.tool {
  width: 44px; height: 44px; display: grid; place-items: center; border: 0; border-radius: 10px; padding: 0;
  background: transparent; color: var(--ink-2); transition: background 0.12s, color 0.12s; flex: none;
}
.tool:hover { background: var(--hover); color: var(--ink); }
.tool.active { background: var(--accent); color: var(--accent-ink); }
.tool canvas { width: 28px; height: 28px; display: block; }
.tool.active canvas { filter: brightness(0) invert(1); }
.tool.add { color: var(--muted); border: 1.5px dashed var(--line); width: 44px; height: 34px; }
.tool.add svg { width: 17px; height: 17px; }
.tool.add:hover, .tool.add.open { color: var(--accent-text); border-color: var(--accent-text); background: var(--accent-soft); }
.sep { display: block; height: 1px; background: var(--line); margin: 4px 6px; flex: none; }
.sep.v { width: 1px; height: 26px; margin: 0 4px; }

/* Verktygsbibliotek */
#library { width: 340px; max-height: min(560px, calc(100dvh - 90px)); display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.lib-head { display: flex; gap: 6px; align-items: center; padding: 10px 10px 8px; border-bottom: 1px solid var(--line); }
.lib-search { flex: 1; display: flex; align-items: center; gap: 8px; background: var(--hover); border-radius: 9px; padding: 0 10px; color: var(--muted); }
.lib-search input { flex: 1; border: 0; background: transparent; outline: none; height: 38px; font-size: 15px; color: var(--ink); min-width: 0; }
#libList { overflow-y: auto; padding: 6px 8px 10px; }
.lib-group { font-size: 12px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin: 10px 6px 6px; }
.lib-item { display: flex; align-items: center; gap: 12px; width: 100%; border: 0; background: transparent; text-align: left; padding: 7px 8px; border-radius: 10px; }
.lib-item:hover { background: var(--hover); }
.lib-prev {
  width: 84px; height: 40px; border-radius: 8px; background: #fff; border: 1px solid var(--line); flex: none;
  display: grid; place-items: center; color: #45526b; overflow: hidden;
}
.lib-prev canvas { width: 84px; height: 40px; display: block; }
.lib-text { flex: 1; min-width: 0; }
.lib-text b { display: block; font-size: 15px; }
.lib-text span { display: block; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-pin { width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center; flex: none; color: var(--muted); border: 1.5px solid var(--line); background: var(--surface); }
.lib-pin svg { width: 16px; height: 16px; }
.lib-pin.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.lib-empty { padding: 24px 12px; text-align: center; color: var(--muted); font-size: 14px; }

/* Nedre panelen */
#props {
  bottom: calc(10px + var(--safe-b)); left: 50%; transform: translateX(-50%);
  max-width: calc(100vw - 20px); overflow-x: auto; scrollbar-width: none;
}
#props::-webkit-scrollbar { display: none; }
.color-btn { width: 40px; height: 40px; border-radius: 10px; border: 0; padding: 5px; background: transparent; flex: none; }
.color-btn:hover { background: var(--hover); }
#colorSwatch { display: block; width: 100%; height: 100%; border-radius: 50%; box-shadow: inset 0 0 0 1.5px rgba(0, 0, 0, 0.15), 0 0 0 2px var(--surface), 0 0 0 3.5px var(--line); }
#quickColors { display: flex; gap: 3px; }
.sw { width: 26px; height: 26px; border-radius: 50%; border: 0; padding: 0; background: var(--c); box-shadow: inset 0 0 0 1px rgba(128, 128, 128, 0.35); flex: none; transition: transform 0.1s; }
.sw:hover { transform: scale(1.12); }
.sw.sel { box-shadow: inset 0 0 0 1px rgba(128, 128, 128, 0.35), 0 0 0 2px var(--surface), 0 0 0 4px var(--accent-text); }
.size-ctl, .zoom-ctl { display: flex; align-items: center; gap: 2px; flex: none; }
#sizeRange { width: 100px; accent-color: var(--accent-text); }
#sizeLabel { min-width: 30px; text-align: center; font-weight: 700; font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.toggle { display: inline-flex; align-items: center; gap: 6px; border: 0; background: var(--hover); border-radius: 9px; height: 34px; padding: 0 10px; font-weight: 700; font-size: 13px; color: var(--ink-2); flex: none; }
.tg-outline { width: 14px; height: 14px; border-radius: 4px; border: 2px solid currentColor; }
.toggle.on { background: var(--accent-soft); color: var(--accent-text); }
.toggle.on .tg-outline { background: currentColor; }
.zoom-label { border: 0; background: transparent; font-weight: 700; font-size: 13px; min-width: 50px; height: 32px; border-radius: 8px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.zoom-label:hover { background: var(--hover); }

.popover {
  position: absolute; z-index: 30; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg);
  border: 1px solid var(--line); padding: 10px; animation: pop 0.14s ease-out;
}
@keyframes pop { from { opacity: 0; translate: 0 6px; } }
.menu { top: calc(62px + var(--safe-t)); left: calc(10px + var(--safe-l)); min-width: 240px; padding: 6px; }
.menu button { display: flex; align-items: center; gap: 10px; width: 100%; border: 0; background: transparent; text-align: left; padding: 10px 12px; border-radius: 9px; font-weight: 600; font-size: 15px; }
.menu button:hover { background: var(--hover); }
.menu button svg { width: 19px; height: 19px; color: var(--ink-2); }
.menu .danger, .menu .danger svg { color: var(--danger); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 6px; }
#userPop { min-width: 200px; }
#userPop .who { display: flex; align-items: center; gap: 10px; padding: 6px 6px 10px; font-weight: 700; }
#userPop .who .av { margin: 0; cursor: default; }
#userPop .tag { font-size: 12px; color: var(--muted); font-weight: 600; display: block; }
#userPop .btn { width: 100%; }

#colorPop { width: 300px; bottom: calc(70px + var(--safe-b)); left: 50%; transform: translateX(-50%); }
#bgPop { width: 280px; top: calc(62px + var(--safe-t)); left: calc(260px + var(--safe-l)); }
.bg-swatches { grid-template-columns: repeat(5, 1fr); }
.bg-swatches .sw { border-radius: 10px; }
.bg-note { margin: 6px 4px 2px; }
.pop-title { font-size: 12px; font-weight: 800; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin: 4px 4px 8px; }
.pop-title .muted { text-transform: none; letter-spacing: 0; font-weight: 600; font-size: 11px; }
.swatches { display: grid; grid-template-columns: repeat(8, 1fr); gap: 7px; margin: 0 4px 12px; }
.swatches .sw { width: 100%; height: auto; aspect-ratio: 1; }
.swatches .add { aspect-ratio: 1; border-radius: 50%; border: 1.5px dashed var(--muted); background: transparent; color: var(--muted); display: grid; place-items: center; padding: 0; }
.swatches .add svg { width: 14px; height: 14px; }
.color-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.custom-color { display: flex; align-items: center; gap: 8px; padding: 8px; border-radius: 9px; cursor: pointer; font-weight: 600; font-size: 14px; border: 0; background: transparent; text-align: left; }
.custom-color:hover { background: var(--hover); }
.custom-color input { width: 30px; height: 30px; border: 0; padding: 0; background: none; border-radius: 8px; cursor: pointer; flex: none; }
.custom-color svg { width: 20px; height: 20px; margin: 0 5px; color: var(--ink-2); }

/* ---------- Matte-tangentbordet (som GeoGebra) ---------- */
.hidden-input { position: fixed; left: 0; bottom: 0; width: 1px; height: 1px; opacity: 0; border: 0; padding: 0; font-size: 16px; resize: none; pointer-events: none; }
#mathKb {
  left: 50%; transform: translateX(-50%); bottom: calc(10px + var(--safe-b)); z-index: 25;
  width: min(660px, calc(100vw - 20px)); padding: 8px;
}
.kb-top { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; }
.kb-tabs { display: flex; gap: 2px; background: var(--hover); border-radius: 9px; padding: 3px; }
.kb-tabs button { border: 0; background: transparent; padding: 6px 12px; border-radius: 7px; font-weight: 700; font-size: 14px; color: var(--ink-2); }
.kb-tabs button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.kb-hint { flex: 1; text-align: right; }
.kb-keys { display: flex; flex-direction: column; gap: 6px; }
.kb-row { display: flex; gap: 6px; }
.kb-row button {
  flex: 1; min-width: 0; height: 42px; border: 0; border-radius: 8px; background: var(--hover); padding: 0;
  font-family: "Cambria Math", "STIX Two Math", "Times New Roman", serif; font-size: 19px; color: var(--ink);
  box-shadow: inset 0 -1px 0 var(--line);
}
.kb-row button:hover { background: var(--accent-soft); }
.kb-row button:active { transform: scale(0.96); }
.kb-row button.num { background: var(--surface); font-family: var(--font); font-weight: 600; box-shadow: inset 0 0 0 1px var(--line); }
.kb-row button.gap { margin-left: 12px; }
.kb-row button.op { font-family: var(--font); }
.kb-row button.act { font-family: var(--font); color: var(--ink-2); }
.kb-row button.enter { background: var(--accent); color: var(--accent-ink); }
.kb-row button.wide { flex: 1.6; }
.kb-row button.on { background: var(--accent-soft); color: var(--accent-text); }
.kb-row sup, .kb-row sub { font-size: 0.6em; }
#boardScreen.math-open #props { display: none; }

#banner {
  position: absolute; z-index: 40; top: calc(66px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  background: var(--tip); color: var(--bg); padding: 9px 16px; border-radius: 10px; font-weight: 700; font-size: 14px; box-shadow: var(--shadow);
}
#toast {
  position: fixed; z-index: 100; left: 50%; bottom: calc(80px + var(--safe-b)); transform: translateX(-50%);
  background: var(--tip); color: var(--bg); padding: 11px 18px; border-radius: 10px; font-weight: 700; font-size: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25); animation: pop 0.16s ease-out; max-width: calc(100vw - 32px); text-align: center;
}
#tip {
  position: fixed; z-index: 120; left: 0; top: 0; pointer-events: none; background: var(--tip); color: var(--bg);
  font-size: 13px; font-weight: 700; padding: 6px 10px; border-radius: 7px; white-space: nowrap; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
}

/* ---------- Dialoger ---------- */
#modalBack { position: fixed; inset: 0; z-index: 60; background: rgba(5, 10, 20, 0.45); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); }
.modal {
  position: fixed; z-index: 61; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(400px, calc(100vw - 32px)); background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 26px;
  box-shadow: var(--shadow-lg); animation: pop 0.16s ease-out;
}
.modal h3 { margin: 0 0 6px; font-size: 22px; letter-spacing: -0.02em; }
.modal .close { position: absolute; top: 12px; right: 12px; }
.modal form { margin-top: 16px; }
.modal .btn.ghost { margin-top: 10px; width: 100%; }
.tabs { display: flex; gap: 4px; background: var(--hover); border-radius: 10px; padding: 4px; margin: 0 40px 14px 0; }
.tabs button { flex: 1; border: 0; background: transparent; padding: 9px; border-radius: 8px; font-weight: 700; color: var(--ink-2); }
.tabs button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }

/* ---------- Mobil (stående) ---------- */
@media (max-width: 760px) {
  #topBar { top: calc(8px + var(--safe-t)); left: calc(8px + var(--safe-l)); right: calc(8px + var(--safe-r)); max-width: none; }
  .invite-label { display: none; }
  .chip.invite { padding-right: 4px; }
  #users { flex: 1; min-width: 0; overflow: hidden; }
  .chip { padding: 0 10px; font-size: 15px; letter-spacing: 0.08em; }
  #tools {
    top: auto; left: calc(8px + var(--safe-l)); right: calc(8px + var(--safe-r));
    bottom: calc(8px + var(--safe-b)); flex-direction: row; overflow-x: auto; overflow-y: hidden; max-height: none;
  }
  #tools .sep { width: 1px; height: auto; margin: 6px 3px; }
  .tool { width: 42px; height: 42px; }
  .tool.add { width: 34px; height: 42px; }
  #props { left: calc(8px + var(--safe-l)); right: calc(8px + var(--safe-r)); transform: none; max-width: none; bottom: calc(66px + var(--safe-b)); }
  #quickColors { display: none; }
  #sizeRange { width: 70px; flex: 1; }
  .size-ctl { flex: 1; }
  .zoom-ctl, .zoom-sep { display: none; }
  .menu { left: auto; right: calc(8px + var(--safe-r)); }
  #bgPop { left: 8px; right: 8px; width: auto; }
  #colorPop { bottom: calc(124px + var(--safe-b)); left: 8px; transform: none; width: calc(100vw - 16px); max-width: 360px; }
  #library { left: 8px !important; right: 8px; top: auto !important; bottom: calc(66px + var(--safe-b)); width: auto; max-height: 60dvh; }
  #toast { bottom: calc(130px + var(--safe-b)); }
  #banner { top: calc(64px + var(--safe-t)); }
  #mathKb { left: 0; right: 0; transform: none; width: auto; bottom: 0; border-radius: 16px 16px 0 0; padding-bottom: calc(8px + var(--safe-b)); z-index: 26; }
  .kb-hint { display: none; }
  .kb-top { justify-content: space-between; }
  .kb-row { gap: 4px; }
  .kb-row button { height: 44px; font-size: 18px; }
  .kb-row button.gap { margin-left: 6px; }
  #boardScreen.math-open #tools { display: none; }
}
@media (max-width: 400px) {
  .chip { padding: 0 8px; font-size: 14px; letter-spacing: 0.04em; }
  .av { width: 30px; height: 30px; }
  .kb-tabs button { padding: 6px 9px; }
}

/* ---------- Liggande mobil / låg skärm ---------- */
@media (max-height: 520px) and (min-width: 761px) {
  .panel { padding: 3px; border-radius: 12px; }
  .tool { width: 36px; height: 36px; border-radius: 9px; }
  .tool svg { width: 19px; height: 19px; }
  .tool canvas { width: 24px; height: 24px; }
  .tool.add { width: 36px; height: 28px; }
  #tools { top: calc(54px + var(--safe-t)); max-height: calc(100dvh - 60px - var(--safe-t) - var(--safe-b)); }
  #topBar { top: calc(6px + var(--safe-t)); }
  .icon-btn { width: 34px; height: 34px; }
  .chip { height: 34px; }
  .btn.small { padding: 6px 10px; }
  #props { bottom: calc(6px + var(--safe-b)); }
  #quickColors { display: none; }
  .color-btn { width: 34px; height: 34px; }
  #colorPop { bottom: calc(56px + var(--safe-b)); max-height: calc(100dvh - 70px); overflow-y: auto; }
  .menu { top: calc(50px + var(--safe-t)); max-height: calc(100dvh - 60px); overflow-y: auto; }
  .kb-row button { height: 34px; }
  #mathKb { bottom: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Koden + Bjud in i samma knapp */
.chip.invite { padding-right: 5px; gap: 10px; }
.invite-tag {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 10px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font-family: var(--font); letter-spacing: 0; font-size: 14px;
}
.invite-tag svg { color: var(--accent-ink); }
.chip.invite:hover .invite-tag { background: var(--accent-hover); }
@media (max-width: 760px) { .invite-tag { padding: 0 8px; } }

/* Påskägg: Thyra → bultande hjärta, Brunost → åsna */
#home .hero-mark svg { transition: color 0.3s; }
#home .hero-mark.love { background: rgba(255, 90, 118, 0.22); border-color: rgba(255, 150, 170, 0.55); transition: background 0.3s; }
#home .hero-mark.love svg { color: #ff4d6d; filter: drop-shadow(0 0 10px rgba(255, 77, 109, 0.7)); animation: beat 0.8s ease-in-out 2; }
@keyframes beat { 0%, 100% { transform: scale(1); } 15% { transform: scale(1.28); } 30% { transform: scale(1); } 45% { transform: scale(1.18); } 60% { transform: scale(1); } }
.av.egg { padding: 0; overflow: hidden; }
.av.egg svg { width: 100%; height: 100%; }
.av.donkey { background: #d6dde7; }
.av.bicep { background: #dbe7f7; }
.av.crown { background: #1e3a6d; }
.av.crown svg { width: 86%; height: 86%; }

/* ---------- Mobil: skriva text och matte så att man ser vad man skriver ---------- */
#textBar {
  position: fixed; z-index: 35; top: calc(8px + var(--safe-t)); left: calc(8px + var(--safe-l)); right: calc(8px + var(--safe-r));
  display: flex; gap: 8px; align-items: flex-end; padding: 8px;
}
#textBarInput {
  flex: 1; min-width: 0; resize: none; border: 1.5px solid var(--line); background: var(--input); color: var(--ink);
  border-radius: 10px; padding: 9px 12px; font-size: 16px; line-height: 1.35; outline: none; max-height: 96px;
}
#textBarInput:focus { border-color: var(--accent-text); box-shadow: 0 0 0 3px var(--accent-soft); }
#boardScreen.text-open #topBar, #boardScreen.text-open #tools, #boardScreen.text-open #props { display: none; }

.math-preview {
  height: 70px; margin-bottom: 8px; border-radius: 10px; background: var(--surface); border: 1.5px solid var(--line);
  overflow: hidden; cursor: text; touch-action: none;
}
.math-preview canvas { display: block; width: 100%; height: 70px; }
#kbCancel svg, #kbDone svg { display: none; }

/* ---------- iPhone/iPad: inga markeringar, ingen sidzoom ---------- */
#boardScreen, #boardScreen * { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
#boardScreen input, #boardScreen textarea { -webkit-user-select: text; user-select: text; }
#boardScreen { touch-action: none; }
#tools { touch-action: pan-x pan-y; }
#props, .kb-tabs { touch-action: pan-x; }
#libList, #library, .menu, #colorPop, #bgPop, #userPop { touch-action: pan-y; }
#boardScreen button { touch-action: manipulation; }
html.board-open, html.board-open body { overflow: hidden; overscroll-behavior: none; }
html.board-open body { position: fixed; inset: 0; }
@media (pointer: coarse) {
  input, textarea, select { font-size: 16px !important; } /* under 16 px zoomar iPhone in hela sidan */
  #textInput { font-size: inherit; }
}

@media (max-width: 760px) {
  .kb-top { gap: 4px; }
  .kb-tabs { flex: 1; }
  .kb-tabs button { flex: 1; padding: 7px 4px; }
  #kbCancel, #kbDone { padding: 7px 10px; }
  #kbCancel span, #kbDone span { display: none; }
  #kbCancel svg, #kbDone svg { display: block; }
  .kb-row button { font-size: 17px; }
  .kb-row button.gap { margin-left: 4px; }
  .math-preview { height: 62px; }
  .math-preview canvas { height: 62px; }
}
@media (max-width: 380px) {
  .kb-row { gap: 3px; }
  .kb-row button { font-size: 15px; height: 40px; }
}
.kb-row button.long { font-size: 14px; letter-spacing: -0.02em; }
@media (max-width: 380px) { .kb-row button.long { font-size: 12px; } }
#libList .row { gap: 6px; }
.lib-item { flex: 1; min-width: 0; width: auto; }

/* Hjälp när länken öppnats i en app (Discord m.fl.) */
#inAppHelp { position: fixed; inset: 0; z-index: 999; background: rgba(5, 12, 28, 0.85); display: grid; place-items: center; padding: 16px; font-family: var(--font); }
#inAppHelp .iah-box { background: #fff; color: #0e1a2f; border-radius: 16px; padding: 22px; max-width: 380px; width: 100%; }
#inAppHelp h3 { margin: 0 0 8px; font-size: 20px; }
#inAppHelp p { margin: 0 0 10px; color: #45526b; line-height: 1.4; }
#inAppHelp input { width: 100%; font-size: 16px; padding: 10px; border: 1.5px solid #dfe4ec; border-radius: 9px; margin-bottom: 10px; }
#inAppHelp button { width: 100%; border: 0; border-radius: 10px; padding: 12px; font-weight: 700; font-size: 16px; background: #1e3a6d; color: #fff; }
#inAppNote {
  position: fixed; z-index: 90; left: 12px; right: 12px; bottom: calc(12px + var(--safe-b)); padding: 10px 40px 10px 14px;
  background: rgba(14, 26, 47, 0.92); color: #fff; border-radius: 12px; font-size: 14px; line-height: 1.35;
}
#inAppNote button { position: absolute; right: 6px; top: 6px; border: 0; background: transparent; color: #fff; font-size: 16px; width: 30px; height: 30px; }

input.zoom-label { width: 58px; text-align: center; border: 1.5px solid transparent; padding: 0 4px; font: inherit; font-weight: 700; font-size: 13px; outline: none; cursor: text; }
input.zoom-label:focus { border-color: var(--accent-text); background: var(--input); color: var(--ink); }
input.zoom-label:hover { border-color: var(--line); background: var(--input); }
#stage.sel { cursor: default; }
