:root {
  --bg: #302e2b; --panel: #262522; --panel-2: #1f1e1c; --line: #3c3a37;
  --text: #ffffff; --muted: #a7a6a2; --green: #81b64c; --green-d: #45753c;
  --light: #ebecd0; --dark: #739552; --hl: rgba(255, 255, 51, .5);
  --sq: min(calc((100dvh - 188px) / 10), calc((100vw - 400px) / 13), 68px);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); min-height: 100%; -webkit-tap-highlight-color: transparent; -webkit-text-size-adjust: 100%; }
button, input { font: inherit; color: inherit; }
button, a { touch-action: manipulation; }
[hidden] { display: none !important; }

.topbar { height: 52px; display: flex; align-items: center; padding: 0 20px; background: var(--panel-2); border-bottom: 1px solid #00000040; }
.brand { color: #fff; text-decoration: none; font-weight: 800; font-size: 20px; letter-spacing: -.3px; display: flex; align-items: center; gap: 10px; }
.logo { width: 30px; height: 30px; border-radius: 7px; background: var(--green); display: grid; place-items: center; font-size: 20px; box-shadow: 0 3px 0 var(--green-d); }

.card { background: var(--panel); border-radius: 10px; box-shadow: 0 6px 24px #0000004d; }
.btn { border: 0; border-radius: 8px; padding: 10px 16px; font-weight: 700; cursor: pointer; background: #3d3b38; transition: filter .12s, transform .06s; }
@media (hover: hover) { .btn:hover { filter: brightness(1.12); } }
.btn:active { transform: translateY(2px); }
.btn.primary { background: var(--green); box-shadow: 0 4px 0 var(--green-d); text-shadow: 0 1px 0 #0003; }
.btn.primary:active { box-shadow: 0 2px 0 var(--green-d); }
.btn.big { font-size: 22px; padding: 18px; width: 100%; border-radius: 12px; margin-top: 8px; }
.btn.big small { display: block; font-size: 13px; font-weight: 500; color: var(--muted); margin-top: 2px; }
.create .btn.ghost.big { margin-top: -6px; padding: 12px; font-size: 17px; }
.btn.ghost { background: #3a3835; color: #d7d6d3; font-weight: 600; }
@media (hover: hover) { .btn.danger:hover { background: #5a2a26; color: #fff; filter: none; } }
.btn:disabled { opacity: .45; pointer-events: none; }

/* Home */
.home { display: flex; gap: 40px; justify-content: center; align-items: center; padding: 40px 20px; min-height: calc(100vh - 52px); flex-wrap: wrap; }
.hero-board { --sq: min(44px, calc((100vw - 40px) / 13)); pointer-events: none; filter: drop-shadow(0 18px 30px #0007); }
.create { width: 420px; max-width: 100%; padding: 28px; display: flex; flex-direction: column; gap: 18px; }
.create h1 { margin: 0; font-size: 30px; line-height: 1.15; letter-spacing: -.5px; }
.sub { margin: -6px 0 0; color: var(--muted); line-height: 1.5; font-size: 15px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field > span { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; }
.field input, .tc-custom input { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 11px 12px; outline: none; }
.field input:focus, .tc-custom input:focus { border-color: var(--green); }
.tc-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.tc-grid button, .seg button { background: #3a3835; border: 2px solid transparent; border-radius: 8px; padding: 10px 4px; cursor: pointer; font-weight: 700; }
.tc-grid button small { display: block; font-weight: 500; color: var(--muted); font-size: 11px; margin-top: 2px; }
.tc-grid button.on, .seg button.on { border-color: var(--green); background: #3f4a33; }
.tc-custom { display: flex; gap: 10px; }
.tc-custom label { flex: 1; display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button { display: flex; align-items: center; justify-content: center; gap: 8px; }
.seg .dot { width: 16px; height: 16px; border-radius: 4px; display: inline-block; border: 1px solid #0006; }
.seg .dot.w { background: #f4f4f4; } .seg .dot.b { background: #2b2b2b; } .seg .dot.r { background: linear-gradient(135deg, #f4f4f4 50%, #2b2b2b 50%); }

/* Board */
.board { display: grid; grid-template-columns: var(--cit, var(--sq)) repeat(11, var(--sq)) var(--cit, var(--sq)); grid-template-rows: repeat(10, var(--sq)); user-select: none; touch-action: none; }
.sq { position: relative; width: var(--sq); height: var(--sq); }
.sq.l { background: var(--light); } .sq.d { background: var(--dark); }
.sq.void { background: transparent; }
.sq.void, .sq.citadel { width: var(--cit, var(--sq)); }
.sq.citadel { background: repeating-linear-gradient(0deg, #9a8a72 0 25%, #8a7a63 25% 26%), #9a8a72;
  background: #a08e72 linear-gradient(#0000 0 48%, #7c6c55 48% 52%, #0000 52%), #a08e72;
  box-shadow: inset 0 0 0 3px #7c6c55, inset 0 0 0 6px #b9a88a; border-radius: 6px; }
.sq.citadel::before { content: "Zitadelle"; position: absolute; bottom: 2px; left: 0; right: 0; text-align: center; font-size: calc(var(--sq) * .14); color: #3d3326; font-weight: 700; }
.board .main-tl { border-top-left-radius: 5px; } .board .main-tr { border-top-right-radius: 5px; }
.board .main-bl { border-bottom-left-radius: 5px; } .board .main-br { border-bottom-right-radius: 5px; }
.coord { position: absolute; font-size: calc(var(--sq) * .2); font-weight: 700; pointer-events: none; line-height: 1; }
.coord.rank { top: 3px; left: 3px; } .coord.file { bottom: 2px; right: 4px; }
.sq.l .coord { color: var(--dark); } .sq.d .coord { color: var(--light); }
.sq.last::after, .sq.sel::after { content: ""; position: absolute; inset: 0; background: var(--hl); }
.sq.check { background-image: radial-gradient(circle, #ff0000 0%, #e70000cc 30%, #a9000000 75%); }
.piece { position: absolute; inset: 4%; z-index: 2; cursor: grab; }
.piece svg { width: 100%; height: 100%; display: block; filter: drop-shadow(0 2px 1px #0000004d); }
.piece .badge { position: absolute; right: -3%; bottom: -2%; width: 40%; height: 40%; border-radius: 50%; background: #ffffffe6; box-shadow: 0 1px 2px #0006; padding: 2px; }
.piece .badge.b { background: #f2f2f2e6; }
.piece .badge svg { filter: none; }
.piece.ghost { position: fixed; pointer-events: none; z-index: 50; width: calc(var(--sq) * 1.1); height: calc(var(--sq) * 1.1); inset: auto; transform: translate(-50%, -50%); cursor: grabbing; }
.piece.ghost.touch { width: calc(var(--sq) * 1.8); height: calc(var(--sq) * 1.8); transform: translate(-50%, -80%); }
.piece.dragging { opacity: .25; }
.hint { position: absolute; inset: 0; z-index: 3; pointer-events: none; display: grid; place-items: center; }
.hint::before { content: ""; width: 34%; height: 34%; border-radius: 50%; background: #00000047; }
.hint.cap::before { width: 92%; height: 92%; background: none; border: calc(var(--sq) * .09) solid #00000047; }
.hint.swap::before { width: 92%; height: 92%; background: none; border: calc(var(--sq) * .07) dashed #2f7bd6cc; }
.sq.hover { box-shadow: inset 0 0 0 calc(var(--sq) * .06) #ffffffaa; }

/* Game */
.game { display: flex; gap: 20px; justify-content: center; padding: 16px 20px; flex-wrap: wrap; }
/* Desktop: Partie passt immer in den Viewport — --sq rechnet Kopfzeile, Spielerzeilen und Seitenleiste ab */
@media (min-width: 1001px) { body:has(#game:not([hidden])) { overflow: hidden; } }
.board-col { display: flex; flex-direction: column; gap: 8px; }
.board-wrap { position: relative; }
.player { display: flex; align-items: center; gap: 10px; height: 44px; padding: 0 calc(var(--sq)); }
.avatar { width: 40px; height: 40px; border-radius: 4px; background: #57534e; position: relative; overflow: hidden; }
.avatar::after { content: ""; position: absolute; inset: 8px 10px 0; border-radius: 50% 50% 0 0; background: #8c8781; }
.player.is-w .avatar { background: #d9d9d9; } .player.is-w .avatar::after { background: #f6f6f6; }
.player.is-b .avatar { background: #3c3a37; } .player.is-b .avatar::after { background: #6a6763; }
.pname { font-weight: 700; font-size: 15px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pstatus { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 5px; }
.pstatus.on::before, .pstatus.off::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--green); }
.pstatus.off::before { background: #777; }
.clock { margin-left: auto; min-width: 120px; text-align: right; font: 700 24px/1 "Roboto Mono", ui-monospace, Menlo, Consolas, monospace; padding: 9px 12px; border-radius: 6px; background: #262421; color: #8b8987; }
.clock.active { background: #fff; color: #262421; }
.clock.low.active { background: #e02828; color: #fff; }
.player.is-b .clock.active { background: #fff; }

.side { width: 340px; display: flex; flex-direction: column; max-height: calc(var(--sq) * 10 + 104px); overflow: hidden; }
.side-head { display: flex; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); font-weight: 700; }
#statusLabel { color: var(--muted); font-weight: 600; }
.moves { flex: 1; overflow-y: auto; font-size: 14px; min-height: 120px; }
.mrow { display: grid; grid-template-columns: 44px 1fr 1fr; padding: 4px 8px; }
.mrow:nth-child(odd) { background: #2b2a27; }
.mrow .n { color: var(--muted); }
.mv { display: inline-flex; align-items: center; gap: 3px; font-weight: 600; padding: 1px 4px; border-radius: 3px; }
.mv.cur { background: #4b4847; border-bottom: 2px solid #ffffff80; }
.mv svg { width: 20px; height: 20px; }
.info { border-top: 1px solid var(--line); padding: 12px 16px; font-size: 14px; line-height: 1.45; min-height: 74px; }
.info b { display: flex; align-items: center; gap: 6px; font-size: 15px; }
.info b svg { width: 24px; height: 24px; }
.info p { margin: 4px 0 0; color: #d0cfcb; }
.muted { color: var(--muted) !important; }
.actions { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.actions .btn { flex: 1; }

/* Overlay */
.overlay { position: absolute; inset: 0 var(--cit, var(--sq)); display: grid; place-items: center; z-index: 20; background: #00000059; border-radius: 6px; }
.modal { background: var(--panel); border-radius: 12px; width: min(380px, 92%); padding: 26px; text-align: center; box-shadow: 0 20px 60px #000a; animation: pop .18s ease-out; }
@keyframes pop { from { transform: scale(.94); opacity: 0; } }
.modal h2 { margin: 0 0 6px; font-size: 26px; }
.modal p { margin: 0 0 18px; color: var(--muted); }
.modal .link { display: flex; gap: 6px; margin-bottom: 14px; }
.modal .link input { flex: 1; min-width: 0; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px; font-size: 13px; }
.modal .btn { width: 100%; }
.modal .row { display: flex; gap: 8px; } .modal .row .btn { flex: 1; }
.modal input.name { width: 100%; margin-bottom: 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 11px 12px; }
.trophy { font-size: 46px; line-height: 1; margin-bottom: 8px; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 4px solid #ffffff22; border-top-color: var(--green); animation: spin 1s linear infinite; margin: 0 auto 14px; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #111; padding: 10px 18px; border-radius: 8px; box-shadow: 0 6px 20px #0008; z-index: 100; font-weight: 600; }

/* Handy & Tablet hochkant: Brett über volle Breite, Seitenleiste darunter */
@media (max-width: 1000px) {
  :root { --sq: min(calc((100vw - 8px) / 13), calc((100dvh - 160px) / 10), 60px); }
  .topbar { height: 44px; padding: 0 12px; }
  .brand { font-size: 17px; } .logo { width: 26px; height: 26px; font-size: 17px; }
  .home { padding: 16px 12px; min-height: auto; }
  .create { padding: 20px; gap: 16px; }
  .create h1 { font-size: 25px; }
  .game { padding: 6px 4px 16px; gap: 10px; }
  .board-col { gap: 4px; }
  .side { width: calc(var(--sq) * 11 + var(--cit, var(--sq)) * 2); max-height: none; }
  .moves { max-height: 34vh; }
  .player { padding: 0 4px; height: 38px; gap: 8px; }
  .avatar { width: 32px; height: 32px; }
  .avatar::after { inset: 6px 8px 0; }
  .clock { font-size: 20px; min-width: 88px; padding: 7px 10px; }
  .modal { padding: 20px; }
  .modal .link input, .field input, .tc-custom input, .modal input.name { font-size: 16px; }
}
/* Handy & Tablet hochkant wie chess.com: Zugleiste oben, Brett randlos, Gangart darunter, Werkzeugleiste unten — nichts scrollt */
@media (max-width: 1000px) and (min-height: 521px) {
  :root { --sq: min(calc((100vw - 8px) / 13), calc((100dvh - 290px) / 10), 60px); }
  body:has(#game:not([hidden])) { overflow: hidden; }
  .game { height: calc(100dvh - 44px); flex-direction: column; flex-wrap: nowrap; justify-content: flex-start; padding: 0; gap: 0; }
  .board-col { order: 2; align-self: center; padding: 4px 0; }
  .side { display: contents; }
  .side-head { display: none; }
  .moves { order: 1; flex: none; height: 36px; min-height: 0; max-height: none; display: flex; align-items: center; overflow-x: auto; overflow-y: hidden; white-space: nowrap; background: var(--panel-2); scrollbar-width: none; }
  .moves::-webkit-scrollbar { display: none; }
  .moves .muted { padding: 0 12px !important; }
  .mrow { display: flex; align-items: center; gap: 4px; padding: 0 6px; }
  .mrow:nth-child(odd) { background: none; }
  .info { order: 3; flex: 1; min-height: 0; overflow-y: auto; border-top: 0; padding: 10px 14px; }
  .actions { order: 4; border-top: 1px solid var(--line); padding: 4px 8px calc(4px + env(safe-area-inset-bottom)); gap: 0; }
  .actions .btn { display: flex; flex-direction: column; align-items: center; gap: 2px; background: none; padding: 6px 4px; font-size: 13px; font-weight: 600; color: var(--muted); }
  .actions .btn .ic { font-size: 22px; line-height: 1; color: #d7d6d3; }
}
@media (max-width: 600px) { .hero { display: none; } }
/* Handy hochkant: Brett randlos, Zitadellen-Spalten halb breit — das 11er-Feld nimmt fast die ganze Breite */
@media (max-width: 600px) {
  :root { --sq: min(calc(100vw / 12), calc((100dvh - 290px) / 10), 60px); }
  .game { --cit: calc(var(--sq) * .5); padding-left: 0; padding-right: 0; }
  .sq.citadel { box-shadow: inset 0 0 0 2px #7c6c55; border-radius: 4px; }
  .sq.citadel::before { display: none; }
}
@media (max-width: 400px) {
  .create { padding: 16px; }
  .tc-grid, .seg { gap: 6px; }
  .tc-grid button, .seg button { padding: 9px 2px; font-size: 15px; white-space: nowrap; }
  .seg button { gap: 5px; }
  .seg .dot { width: 13px; height: 13px; }
}
/* Handy quer: Kopfzeile weg, Brett nach Höhe, Seitenleiste daneben */
@media (orientation: landscape) and (max-height: 520px) {
  :root { --sq: min(calc((100dvh - 84px) / 10), calc((100vw - 260px) / 13)); }
  body:has(#game:not([hidden])) .topbar { display: none; }
  .game { flex-wrap: nowrap; padding: 4px; gap: 8px; }
  .board-col { gap: 4px; }
  .player { height: 34px; }
  .avatar { width: 28px; height: 28px; }
  .clock { font-size: 17px; min-width: 72px; padding: 6px 8px; }
  .side { width: auto; flex: 1; min-width: 220px; max-width: 340px; max-height: calc(100dvh - 8px); }
  .moves { max-height: none; min-height: 0; }
  .info { min-height: 0; padding: 8px 12px; font-size: 13px; }
  .side-head { padding: 8px 12px; font-size: 14px; }
  .actions { padding: 8px 12px; }
}
a.btn { display: inline-flex; align-items: center; justify-content: center; color: #fff; text-decoration: none; }
.hero-board { --sq: min(52px, calc((100vw - 40px) / 13)); }
