:root {
  --bg: #f4efe6;
  --surface: #fffdf8;
  --surface-2: #ebe4d6;
  --ink: #1d1b18;
  --muted: #6d665b;
  --line: #d9d0bf;
  --accent: #2f5d50;
  --accent-ink: #ffffff;
  --accent-soft: #dce8e3;
  --danger: #a33a2c;
  --gold: #b8862b;
  --white-piece: #ffffff;
  --black-piece: #1d1b18;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15140f;
    --surface: #1f1d17;
    --surface-2: #2a2720;
    --ink: #efe9dc;
    --muted: #a39b8c;
    --line: #3a362d;
    --accent: #6fae98;
    --accent-ink: #10201b;
    --accent-soft: #233b33;
    --danger: #e0715f;
    --gold: #d9a54a;
    --black-piece: #0b0a08;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 var(--sans);
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.15; margin: 0 0 .5em; }
h1 { font-size: 2rem; }
h2 { font-size: 1.3rem; }
p { margin: 0 0 1em; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.error { color: var(--danger); }

.page {
  max-width: 640px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px calc(32px + env(safe-area-inset-bottom));
}
.topbar {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 16px; min-height: 44px;
}
.topbar .title { flex: 1; font-family: var(--serif); font-size: 1.15rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; }
.brand img { width: 36px; height: 36px; border-radius: 9px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.hero {
  background: var(--accent);
  color: var(--accent-ink);
  border: 0;
}
.hero h2 { font-size: 1.6rem; }
.hero p { opacity: .9; }
.hero .btn { background: var(--accent-ink); color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px;
  border-radius: 999px; border: 1px solid transparent;
  background: var(--accent); color: var(--accent-ink);
  font: 600 1rem var(--sans); text-decoration: none; cursor: pointer;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn.secondary { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn.ghost { background: transparent; color: var(--ink); border-color: transparent; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.block { width: 100%; }
.btn.icon { padding: 0; width: 44px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.row > .grow { flex: 1; }
.stack > * + * { margin-top: 12px; }

label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; }
input, select {
  width: 100%; min-height: 46px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); color: var(--ink);
  font: 1rem var(--sans);
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field { margin-bottom: 14px; }
.field .hint { font-size: .8rem; color: var(--muted); margin-top: 4px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li + li { border-top: 1px solid var(--line); }
.list a, .list .item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 0; color: inherit; text-decoration: none;
}
.list .grow { flex: 1; min-width: 0; }
.list .sub { font-size: .85rem; color: var(--muted); }
.chev { color: var(--muted); }

/* Pjäser */
.pc { font-family: "Segoe UI Symbol", "Noto Sans Symbols 2", "DejaVu Sans", sans-serif; line-height: 1; }
.pc.white { color: var(--white-piece); -webkit-text-stroke: 1.2px var(--black-piece); paint-order: stroke fill; text-shadow: 0 0 1px var(--black-piece); }
.pc.black { color: var(--black-piece); }

/* Tabell */
table.standings { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.standings th, .standings td { padding: 10px 6px; text-align: right; }
.standings th { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
.standings th:nth-child(2), .standings td:nth-child(2) { text-align: left; }
.standings td:first-child { color: var(--muted); width: 1.5em; }
.standings tbody tr + tr td { border-top: 1px solid var(--line); }
.standings .pts { font-weight: 700; font-size: 1.05rem; }
.standings tr.me td:nth-child(2) { font-weight: 700; }
.table-wrap { overflow-x: auto; margin: 0 -6px; }
@media (max-width: 420px) { .standings .opt { display: none; } }

/* Partivy */
.game {
  display: flex; flex-direction: column;
  height: 100dvh;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
  max-width: 720px; margin: 0 auto;
}
.game .topbar { padding: 8px 12px 0; margin: 0; }
.side {
  flex: 1; display: flex; flex-direction: column; justify-content: center; gap: 10px;
  padding: 12px 16px; min-height: 0;
}
.side.flipped { transform: rotate(180deg); }
.side-head { display: flex; align-items: center; gap: 12px; }
.side-head .who { flex: 1; min-width: 0; }
.side-head .name { font-family: var(--serif); font-size: 1.35rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-head .role { font-size: .8rem; color: var(--muted); }
.side-head .king { font-size: 2.2rem; }
.score { font-family: var(--serif); font-size: 3rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1; }
.score.lead { color: var(--accent); }
.trophies { min-height: 28px; font-size: 1.5rem; display: flex; flex-wrap: wrap; align-items: center; gap: 2px; }
.trophies .muted { font-size: .85rem; }
.bonus { font: 700 .8rem var(--sans); background: var(--gold); color: #fff; border-radius: 999px; padding: 3px 10px; margin-left: 8px; }
.pieces { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.piece {
  position: relative; aspect-ratio: 1 / 1; max-height: 84px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 14px; border: 1px solid var(--line);
  background: var(--surface); box-shadow: var(--shadow);
  font-size: clamp(2rem, 9vw, 3rem); cursor: pointer; padding: 0;
  transition: transform .08s;
}
.piece:active:not(:disabled) { transform: scale(.94); }
.piece:disabled { opacity: .3; cursor: default; box-shadow: none; }
.piece .count {
  position: absolute; right: 4px; bottom: 3px;
  font: 700 .75rem var(--sans); color: var(--muted);
}
.piece .val {
  position: absolute; left: 6px; top: 4px;
  font: 600 .7rem var(--sans); color: var(--muted);
}
.hint-line { font-size: .8rem; color: var(--muted); text-align: center; margin: 0; }
.midbar {
  display: flex; align-items: center; gap: 8px; justify-content: center; flex-wrap: wrap;
  padding: 10px 12px; background: var(--surface-2); border-block: 1px solid var(--line);
}
.midbar .diff { font-weight: 700; min-width: 5em; text-align: center; }
.result-banner { text-align: center; padding: 14px 16px; background: var(--accent-soft); border-block: 1px solid var(--line); }
.result-banner strong { font-family: var(--serif); font-size: 1.3rem; display: block; }
.result-banner .row { justify-content: center; margin-top: 10px; }

/* Dialog */
dialog {
  border: 0; border-radius: 18px; padding: 0;
  background: var(--surface); color: var(--ink);
  width: min(420px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
#modal-body { padding: 20px; }
.choice-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 12px 0; }
.choice-grid .btn { border-radius: 12px; min-height: 56px; }
.choice-grid .btn .pc { font-size: 1.8rem; }
.qr { display: flex; justify-content: center; margin: 12px 0; }
.qr svg { width: 220px; height: 220px; background: #fff; border-radius: 10px; padding: 6px; }
.linkbox { font-size: .85rem; word-break: break-all; background: var(--surface-2); padding: 8px 10px; border-radius: 8px; }
.code { font: 700 1.6rem ui-monospace, monospace; letter-spacing: .15em; text-align: center; }

/* Toast */
#toast {
  position: fixed; left: 50%; top: calc(56px + env(safe-area-inset-top));
  width: max-content; font-size: .9rem;
  transform: translate(-50%, -12px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg);
  padding: 10px 12px 10px 18px; border-radius: 999px;
  display: flex; align-items: center; gap: 12px;
  transition: opacity .2s, transform .2s; z-index: 10; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { background: transparent; color: var(--bg); border: 1px solid currentColor; border-radius: 999px; padding: 4px 12px; font: 600 .85rem var(--sans); cursor: pointer; }

.badge { display: inline-block; font: 600 .7rem var(--sans); padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); vertical-align: middle; }
.empty { color: var(--muted); font-size: .9rem; padding: 8px 0; }
.section-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.section-title h2 { margin: 0; }

/* Mörkt läge: ljusa "rutor" bakom pjäserna så att svarta pjäser syns */
@media (prefers-color-scheme: dark) {
  .pc.black { -webkit-text-stroke: 1.4px #d8cfbd; paint-order: stroke fill; }
  .piece { background: #cdc2ad; border-color: #a99e88; }
  .piece:disabled { opacity: .25; }
  .piece .count, .piece .val { color: #3d382f; }
  .piece .pc.black { -webkit-text-stroke: 0; }
}
