:root {
  --c: 56px;
  --bg: #efe8dc;
  --card: #fffdf8;
  --ink: #2c3440;
  --muted: #6b7280;
  --line: #ddd3c2;
  --accent: #2563eb;
  --table: #1e6b3c;
  --space: #14234a;
  --ok: #16a34a;
  --bad: #dc2626;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  font-family: "Prata", Georgia, serif;
  color: var(--ink);
  background: var(--bg);
}

h1, h2, h3 { margin: 0; font-family: "Uncial Antiqua", "Prata", Georgia, serif; font-weight: normal; }
h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }

button {
  font: inherit;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { border-color: #b9ab92; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}

/* ---------- Overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background: rgba(40, 30, 20, .45);
}
.overlay[hidden] { display: none; }
.start-card { width: min(420px, calc(100vw - 32px)); padding: 28px; display: grid; gap: 14px; text-align: center; }
.start-card h1 { font-size: 32px; }
.sub { margin: -8px 0 0; color: var(--muted); }
.start-card label { display: flex; justify-content: space-between; align-items: center; gap: 12px; text-align: left; }
.start-card select, .start-card input { font: inherit; padding: 4px 8px; width: 90px; }
.meeple-row { display: flex; justify-content: center; gap: 8px; }
.meeple-row img { width: 48px; }
.btn-row { display: flex; gap: 8px; justify-content: center; }
#ranking { text-align: left; font-size: 18px; padding-left: 24px; }
#ranking li { margin: 6px 0; }

/* ---------- Layout ---------- */
#game { height: 100%; display: flex; flex-direction: column; }
#game[hidden] { display: none; }
header {
  display: flex; align-items: center; gap: 16px;
  padding: 8px 16px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
header h1 { font-size: 20px; }
#status { flex: 1; color: var(--muted); }
.hdr-btns { display: flex; gap: 6px; }

main { flex: 1; display: flex; min-height: 0; position: relative; }
#side {
  width: 320px; flex-shrink: 0;
  position: relative; z-index: 1;
  background: var(--bg);
  overflow-y: auto;
  padding: 12px;
  display: flex; flex-direction: column; gap: 10px;
}
#boardWrap {
  flex: 1; overflow: auto;
  display: flex; padding: 24px;
  position: relative; z-index: 1;
  background: var(--space);
}

#board {
  position: relative;
  margin: auto; flex-shrink: 0;
  user-select: none;
}

@media (max-width: 760px) {
  main { flex-direction: column-reverse; }
  #side { width: auto; max-height: 45vh; }
}

/* ---------- Ein-/ausklappbare Bereiche ---------- */
.fold[hidden] { display: none; }
.fold summary { list-style: none; cursor: pointer; display: flex; align-items: center; gap: 6px; }
.fold summary::-webkit-details-marker { display: none; }
.fold summary h3 { margin: 0; flex: 1; }
.fold summary::after { content: "▾"; color: var(--muted); transition: transform .15s ease; }
.fold:not([open]) summary::after { transform: rotate(-90deg); }
.fold[open] summary { margin-bottom: 8px; }

/* ---------- Turn panel ---------- */
#turn { border-width: 2px; }
.turn-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.turn-head img { width: 34px; }
.turn-head b { font-size: 18px; font-family: "Uncial Antiqua", "Prata", Georgia, serif; font-weight: normal; }
.phase { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.instr { margin: 6px 0 10px; line-height: 1.4; font-size: 14px; }
.instr.warn { color: #b45309; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }

/* ---------- Hand ---------- */
#hand { display: flex; gap: 8px; flex-wrap: wrap; min-height: 40px; }
#hand .htile {
  position: relative;
  width: 110px; height: 59px; padding: 0; border-radius: 10px;
  border: 2px solid transparent; cursor: pointer;
  background: rgba(0,0,0,.06);
}
/* Senkrecht gedrehte Vorschau: Kasten hochkant, Stein rotiert um seine Mitte */
#hand .htile.rot1, #hand .htile.rot3 { width: 59px; height: 110px; }
#hand .htile .domino {
  position: absolute; left: 50%; top: 50%;
  width: 96px; height: 48px;
  transform: translate(-50%, -50%) rotate(var(--rot));
  transition: transform .15s ease;
}
/* Markiert die Steinhälfte, die auf dem angetippten Feld landet */
#hand .htile .anchor-dot {
  position: absolute; left: 50%; top: 50%;
  width: 18px; height: 18px; border-radius: 50%;
  background: rgba(255,255,255,.55); border: 2px solid #000;
  transform: translate(-50%, -50%) rotate(var(--rot)) translateX(-24px);
  transition: transform .15s ease; pointer-events: none;
}
#hand .htile.sel { border-color: var(--accent); box-shadow: 0 0 0 2px #fff inset; }
#hand .empty { color: var(--muted); font-size: 13px; }

/* ---------- Dice ---------- */
#dice { display: flex; flex-wrap: wrap; gap: 8px; min-height: 40px; }
.die {
  width: 38px; height: 38px; padding: 5px;
  background: linear-gradient(145deg, #fff, #ecebe7);
  border: 1px solid #cfcac0; border-radius: 8px;
  display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  box-shadow: 0 2px 3px rgba(0,0,0,.18);
}
button.die { cursor: pointer; }
.die.sel { outline: 3px solid var(--accent); outline-offset: 2px; }
.die.dead { opacity: .18; }
.die span { width: 72%; height: 72%; margin: auto; border-radius: 50%; }
.die.small { width: 22px; height: 22px; padding: 2px; border-radius: 5px; }

/* ---------- Players ---------- */
.player {
  display: grid; grid-template-columns: 30px 1fr auto; gap: 2px 8px; align-items: center;
  padding: 6px; border-radius: 8px;
}
.player.active { background: #f3ede2; }
.player img { width: 30px; grid-row: span 2; }
.player .name { font-weight: 600; }
.ai-tag { font-size: 10px; font-weight: 700; background: #2c3440; color: #fff; border-radius: 4px; padding: 1px 4px; vertical-align: middle; }
.player .score { font-size: 20px; font-weight: 700; grid-row: span 2; }
.player .meta { font-size: 12px; color: var(--muted); }

#log { margin: 0; padding-left: 18px; font-size: 12.5px; max-height: 180px; overflow-y: auto; color: #444; }
#log li { margin: 2px 0; }
#log li.round { list-style: none; margin-left: -18px; font-weight: 600; color: var(--ink); }

/* ---------- Board pieces ---------- */
.tile {
  position: absolute; padding: 2px;
  transform-origin: center;
  transform: rotate(var(--rot));
  pointer-events: none;
}
.tile.lifted { opacity: .35; }
.tile.ghost { opacity: .85; z-index: 5; }
.tile.ghost.ok .domino { outline: 3px solid var(--accent); }
.tile.ghost.bad .domino { outline: 3px solid var(--bad); opacity: .6; }

/* Dominostein-Grafik: die JPGs haben schwarze Ecken ausserhalb des
   abgerundeten Steins (Radius ≈ 58 px bei 800×400), daher hier abschneiden */
.domino {
  display: block;
  width: 100%; height: 100%;
  border-radius: 7.5% / 15%;
  box-shadow: 0 3px 6px rgba(0,0,0,.45);
  user-select: none;
}

.mark { position: absolute; pointer-events: none; border-radius: 6px; z-index: 2; }
.mark.target { background: rgba(255,255,255,.25); box-shadow: inset 0 0 0 2px rgba(255,255,255,.9); }
.mark.anchor::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 30%; height: 30%; transform: translate(-50%, -50%);
  border-radius: 50%; background: rgba(255,255,255,.85); box-shadow: 0 0 0 2px rgba(0,0,0,.15);
}
.mark.pick { box-shadow: inset 0 0 0 3px rgba(234,179,8,.95); background: rgba(250,204,21,.2); }
.mark.chosen { box-shadow: inset 0 0 0 3px var(--accent); background: rgba(37,99,235,.2); }

/* Zauberblöcke: transparente, farbige Kunststoffwürfel mit Spiegelungen */
.cube {
  position: absolute; z-index: 3; pointer-events: none;
  border-radius: 4px;
  border: 1px solid color-mix(in srgb, var(--glow) 60%, #fff);
  background:
    linear-gradient(135deg, rgba(255,255,255,.55) 0 7%, transparent 18%),
    linear-gradient(315deg, rgba(0,0,0,.25) 0 10%, transparent 30%),
    color-mix(in srgb, var(--glow) 45%, transparent);
  box-shadow:
    inset 0 0 0 2px color-mix(in srgb, var(--glow) 55%, transparent),
    inset 3px 3px 2px rgba(255,255,255,.35),
    0 0 8px color-mix(in srgb, var(--glow) 60%, transparent),
    0 3px 5px rgba(0,0,0,.35);
  animation: hover 2.4s ease-in-out infinite;
}
.cube.el-fire { animation: hover 2.4s ease-in-out infinite, flicker .9s ease-in-out infinite; }
.cube.el-lightning { animation: hover 2.4s ease-in-out infinite, zap 1.7s steps(1) infinite; }
@keyframes hover { 0%, 100% { translate: 0 0; } 50% { translate: 0 -2px; } }
@keyframes flicker { 0%, 100% { filter: none; } 50% { filter: brightness(1.25) drop-shadow(0 0 5px #ff9a2e); } }
@keyframes zap { 0%, 88%, 100% { filter: none; } 90%, 94% { filter: brightness(1.8); } }

/* ---------- Erscheinen-Animationen ---------- */
.tile.appear { animation: tileDrop .55s cubic-bezier(.3, 1.6, .5, 1) both; }
@keyframes tileDrop {
  0% { transform: rotate(var(--rot)) translateY(-40px) scale(1.35); opacity: 0; filter: brightness(2); }
  60% { opacity: 1; }
  100% { transform: rotate(var(--rot)); filter: none; }
}
.cube.appear { animation: cubeSummon .6s cubic-bezier(.3, 1.7, .5, 1) both, hover 2.4s ease-in-out .6s infinite; }
@keyframes cubeSummon {
  0% { transform: scale(0) rotate(-200deg); opacity: 0; filter: brightness(3); }
  70% { transform: scale(1.3) rotate(15deg); opacity: 1; }
  100% { transform: scale(1) rotate(0); filter: none; }
}
.meeple.appear { animation: meeplePoof .65s cubic-bezier(.3, 1.8, .5, 1) both; }
@keyframes meeplePoof {
  0% { transform: translateY(-30px) scale(0) rotate(-30deg); opacity: 0; }
  55% { transform: translateY(4px) scale(1.25, .85) rotate(8deg); opacity: 1; }
  75% { transform: translateY(-4px) scale(.95, 1.08); }
  100% { transform: none; }
}
.sparkle { position: absolute; z-index: 6; pointer-events: none; width: 0; height: 0; }
.sparkle i {
  position: absolute; left: -4px; top: -4px; width: 8px; height: 8px;
  background: var(--spark);
  clip-path: polygon(50% 0, 62% 38%, 100% 50%, 62% 62%, 50% 100%, 38% 62%, 0 50%, 38% 38%);
  filter: drop-shadow(0 0 3px #fff);
  animation: spark .6s ease-out both;
}
@keyframes spark {
  0% { transform: translate(0, 0) scale(.3) rotate(0); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.2) rotate(180deg); opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .cube, .appear, .sparkle i { animation: none !important; }
}
.meeple { position: absolute; z-index: 4; pointer-events: none; filter: drop-shadow(0 3px 2px rgba(0,0,0,.45)); }
.badge {
  position: absolute; z-index: 5; pointer-events: none;
  background: #111; color: #fff; font-size: 11px; font-weight: 700;
  border-radius: 9px; padding: 0 5px; line-height: 16px;
}

/* ---------- Rules ---------- */
dialog#rules {
  width: min(680px, calc(100vw - 32px)); max-height: 85vh;
  border: none; border-radius: 14px; padding: 24px;
  line-height: 1.45;
}
dialog#rules::backdrop { background: rgba(40, 30, 20, .45); }
dialog#rules h3 { margin-top: 16px; }
dialog#rules ul, dialog#rules ol { padding-left: 22px; }
kbd { border: 1px solid #ccc; border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; font-size: 12px; }
