:root {
  --bg: #15171c;
  --bg2: #1d2027;
  --panel: #232731;
  --panel2: #2b303c;
  --line: #3a4150;
  --text: #e9ebf0;
  --muted: #9aa3b2;
  --accent: #f5b800;
  --good: #58d68d;
  --bad: #ff5d5d;
  --throw: #4aa8ff;
  --control: #b266ff;
  --swap: #ff9a3c;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(1200px 700px at 50% -10%, #272c38 0%, var(--bg) 60%);
  color: var(--text);
  font: 15px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
h1, h2, h3 { margin: 0; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 16px; background: #101217; border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 20px; letter-spacing: .3px; }
.brand h1 span { color: var(--accent); font-weight: 600; }
nav { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  background: var(--panel2); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 7px 13px; font: inherit; cursor: pointer;
  transition: background .15s, transform .05s;
}
.btn:hover { background: #363c4a; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--accent); color: #1a1a1a; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { background: #ffc928; }
.btn.block { display: block; width: 100%; margin-top: 8px; text-align: left; }

.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 330px; gap: 12px; padding: 12px; }
.stage { min-width: 0; min-height: 0; display: flex; flex-direction: column; gap: 8px; }
.board-wrap {
  position: relative; flex: 1; min-height: 0; border-radius: 14px; overflow: hidden;
  border: 1px solid var(--line);
  background:
    radial-gradient(circle at 50% 40%, #2b303b 0%, #1b1e25 80%);
}
#board { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; user-select: none; }
#board.dragging { cursor: grabbing; }

.side { min-height: 0; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); margin-bottom: 8px; }
.card.history { flex: 1; min-height: 140px; display: flex; flex-direction: column; }
#log { margin: 0; padding: 0 0 0 4px; list-style: none; overflow-y: auto; flex: 1; font-size: 13px; }
#log li { padding: 2px 6px; border-radius: 5px; display: flex; gap: 6px; }
#log li:nth-child(odd) { background: rgba(255,255,255,.03); }
#log li.last { background: rgba(245,184,0,.16); }
#log .n { color: var(--muted); min-width: 26px; text-align: right; }
#log .w { width: 10px; height: 10px; border-radius: 50%; margin-top: 6px; flex: none; border: 1px solid #000; }

/* status */
.turnline { display: flex; align-items: center; gap: 10px; font-size: 16px; font-weight: 600; }
.dot { width: 16px; height: 16px; border-radius: 50%; border: 2px solid #000; flex: none; }
.dot.w { background: #f3ecd9; } .dot.b { background: #2b2d33; border-color: #777; }
.sub { color: var(--muted); font-size: 13px; margin-top: 4px; }
.qrow { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 13px; }
.pips { display: flex; gap: 3px; }
.pip { width: 11px; height: 11px; transform: rotate(45deg); border-radius: 2px; background: #3a4150; }
.pip.on { background: var(--bad); }
.thinking { color: var(--accent); }

/* info */
.infohead { display: flex; gap: 12px; align-items: center; }
.infohead svg { flex: none; }
.infohead b { font-size: 16px; }
.infohead small { color: var(--muted); display: block; }
.infotext { margin: 8px 0 0; font-size: 13.5px; color: #cfd4de; }
.infoback { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); font-size: 13px; color: #cfd4de; }
.infoback b { color: #9be15d; }

/* mãos */
.hand {
  display: flex; flex-direction: column; gap: 4px; min-height: 66px;
  padding: 6px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
}
.hand .tiles { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.hand .who { font-size: 12px; text-transform: uppercase; letter-spacing: .7px; color: var(--muted); width: 100%; display: flex; justify-content: space-between; }
.hand.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.htile {
  position: relative; background: transparent; border: 0; padding: 0; cursor: pointer;
  border-radius: 10px; width: 56px; height: 56px; transition: transform .1s;
}
.htile:hover { transform: translateY(-2px); }
.htile.sel { filter: drop-shadow(0 0 6px var(--accent)); transform: translateY(-4px); }
.htile.dim { opacity: .55; cursor: default; }
.htile.steal { outline: 2px dashed var(--swap); outline-offset: -2px; }
.htile .cnt {
  position: absolute; right: -2px; bottom: -2px; background: #000; color: #fff; font-size: 11px;
  min-width: 18px; height: 18px; border-radius: 9px; display: grid; place-items: center; padding: 0 4px; border: 1px solid #555;
}
.htile .flip {
  position: absolute; left: -3px; bottom: -3px; width: 20px; height: 20px; border-radius: 50%;
  background: #3b6d1c; color: #fff; border: 1px solid #000; font-size: 12px; line-height: 18px; text-align: center; cursor: pointer;
}
.htile .flip:hover { background: #55a028; }
.handinfo { font-size: 12px; color: var(--muted); margin-left: auto; align-self: flex-end; }

/* destaques do tabuleiro */
.hl { cursor: pointer; }
.hl .hl-hex { fill: transparent; stroke-width: 3.5; stroke-linejoin: round; }
.hl .hl-dot { stroke: rgba(0,0,0,.5); stroke-width: 1.5; }
.hl.move .hl-hex { stroke: var(--good); } .hl.move .hl-dot { fill: rgba(88,214,141,.75); }
.hl.attack .hl-hex { stroke: var(--bad); stroke-width: 4; } .hl.attack .hl-dot { fill: rgba(255,93,93,.85); }
.hl.throw .hl-hex { stroke: var(--throw); stroke-width: 4; } .hl.throw .hl-dot { fill: rgba(74,168,255,.85); }
.hl.control .hl-hex { stroke: var(--control); stroke-width: 4; } .hl.control .hl-dot { fill: rgba(178,102,255,.85); }
.hl.swap .hl-hex { stroke: var(--swap); stroke-width: 4; } .hl.swap .hl-dot { fill: rgba(255,154,60,.85); }
.hl.occ .hl-dot { display: none; }
.hl.empty .hl-hex { fill: rgba(255,255,255,.04); stroke-dasharray: 5 4; stroke-width: 2; }
.hl:hover .hl-hex { fill: rgba(255,255,255,.14); stroke-dasharray: none; }
.hl .hl-dot { animation: pulse 1.4s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
@keyframes pulse { 50% { transform: scale(1.25); } }
.selring { fill: none; stroke: var(--accent); stroke-width: 4; stroke-linejoin: round; filter: drop-shadow(0 0 5px var(--accent)); pointer-events: none; }
.lastmv { fill: rgba(245,184,0,.18); stroke: rgba(245,184,0,.55); stroke-width: 2; stroke-linejoin: round; pointer-events: none; }
.piece { cursor: pointer; }
.piece.mine:hover { filter: brightness(1.12); }

.chooser {
  position: absolute; z-index: 5; background: #11141a; border: 1px solid var(--line); border-radius: 10px;
  padding: 6px; display: flex; flex-direction: column; gap: 4px; box-shadow: 0 10px 30px rgba(0,0,0,.5);
}
.chooser button { text-align: left; }
.toast {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%); z-index: 6;
  background: rgba(10,12,16,.92); border: 1px solid var(--line); padding: 8px 14px; border-radius: 999px; font-size: 14px;
  pointer-events: none; max-width: 90%; text-align: center;
}

/* diálogos */
dialog {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 14px;
  padding: 20px 22px; max-width: 560px; width: calc(100% - 24px);
}
dialog.wide { max-width: 860px; max-height: 88vh; overflow-y: auto; }
dialog::backdrop { background: rgba(0,0,0,.65); }
dialog h2 { margin-bottom: 10px; }
fieldset { border: 1px solid var(--line); border-radius: 10px; margin: 10px 0; padding: 8px 12px 10px; }
legend { padding: 0 6px; color: var(--accent); font-size: 13px; text-transform: uppercase; letter-spacing: .7px; }
label.radio, label.check { display: block; padding: 3px 0; cursor: pointer; }
label small { color: var(--muted); }
fieldset .row { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); }
fieldset .row label { display: inline-block; }
menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 14px 0 0; }
.rules h3 { margin: 16px 0 6px; color: var(--accent); font-size: 15px; }
.rules p, .rules li { font-size: 14px; color: #d6dae3; }
.rules ul { padding-left: 20px; margin: 6px 0; }
.rules code { background: #11141a; padding: 1px 5px; border-radius: 4px; }
.rules .note { background: rgba(245,184,0,.1); border: 1px solid rgba(245,184,0,.35); border-radius: 8px; padding: 8px 10px; }
.piece-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 8px; }
.piece-item { display: flex; gap: 10px; background: var(--panel2); border-radius: 10px; padding: 8px; align-items: flex-start; }
.piece-item svg { flex: none; }
.piece-item b { display: block; font-size: 14px; }
.piece-item span { font-size: 12.5px; color: #c4cad6; }
.piece-item em { color: #9be15d; font-style: normal; font-size: 12px; }

/* relógio */
.hand .who { align-items: center; }
.whoR { display: flex; align-items: center; gap: 8px; }
.clock {
  font: 700 20px/1 "Consolas", "SFMono-Regular", ui-monospace, monospace; background: #11141a; color: #cfd4de;
  border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; min-width: 76px; text-align: center;
  letter-spacing: .5px;
}
.clock.run { background: #f3ecd9; color: #1a1a1a; border-color: #fff; }
.clock.low { color: #ff7b7b; }
.clock.run.low { background: #ff5d5d; color: #fff; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .6; } }

/* chip de turno e barra de info sobre o tabuleiro */
.chip {
  position: absolute; left: 10px; top: 10px; z-index: 4; pointer-events: none;
  background: rgba(10,12,16,.82); border: 1px solid var(--line); border-radius: 999px;
  padding: 5px 12px; font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 7px;
}
.chip .dot { width: 12px; height: 12px; }
.infobar {
  display: none; position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 4; pointer-events: none;
  background: rgba(10,12,16,.9); border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px;
  font-size: 12.5px; line-height: 1.35; color: #d6dae3;
}
.infobar b { color: var(--accent); }

/* sala online */
.input {
  width: 100%; background: #11141a; color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font: inherit; font-size: 16px;
}
#joinCode { text-align: center; font: 700 28px/1.2 "Consolas", ui-monospace, monospace; letter-spacing: 8px; text-transform: uppercase; }
.roomcode {
  text-align: center; font: 800 44px/1.1 "Consolas", ui-monospace, monospace; letter-spacing: 10px; color: var(--accent);
  background: #11141a; border: 1px dashed var(--line); border-radius: 12px; padding: 14px 6px; margin: 10px 0;
}
.select {
  width: 100%; background: #11141a; color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; font: inherit; font-size: 15px;
}
#gameBtns { display: flex; gap: 8px; padding: 8px 12px; }
#gameBtns[hidden], #actions[hidden], #infoBar[hidden], #btnUndo[hidden] { display: none !important; }


/* ---------- sobreposições no tabuleiro ---------- */
.overlay-bottom {
  position: absolute; left: 8px; right: 8px; bottom: 8px; z-index: 4; pointer-events: none;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.floatbar { pointer-events: auto; display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; max-width: 100%; }
.floatbar .btn { background: rgba(17,20,26,.94); border-color: var(--accent); box-shadow: 0 4px 14px rgba(0,0,0,.45); }
.floatbar .hint { background: rgba(10,12,16,.9); border: 1px solid var(--line); border-radius: 10px; padding: 5px 10px; font-size: 12.5px; color: var(--muted); }
.floatbar[hidden], .infobar[hidden] { display: none !important; }
.infobar { display: none; position: static; max-width: 560px; }
.infobar:not([hidden]) { display: block; }

.brushbar {
  position: absolute; right: 10px; top: 10px; z-index: 4; display: flex; gap: 6px; padding: 4px 6px;
  background: rgba(10,12,16,.82); border: 1px solid var(--line); border-radius: 999px; align-items: center;
}
.brush {
  width: 22px; height: 22px; border-radius: 50%; border: 2px solid rgba(255,255,255,.25); padding: 0;
  background: var(--c, #555); cursor: pointer; flex: none;
}
.brush.sel { border-color: #fff; box-shadow: 0 0 0 2px rgba(255,255,255,.25); }
.brush.clear { background: transparent; color: #cfd4de; font-size: 12px; line-height: 18px; text-align: center; }
.shape-arrow { pointer-events: none; }

.nav-mobile, .sheet-head, .side-backdrop { display: none; }
.nav-desktop { display: flex; gap: 8px; flex-wrap: wrap; }
.hand .tiles::-webkit-scrollbar { height: 4px; }

/* tempo personalizado */
.customtime { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); display: grid; gap: 10px; }
.customtime[hidden], .customtime .diff[hidden] { display: none !important; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid2 label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.pair { display: flex; align-items: center; gap: 6px; color: var(--text); }
.pair input {
  width: 64px; background: #11141a; color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 8px; font: inherit; font-size: 16px; text-align: center;
}
.menulist { display: grid; gap: 8px; }
.menulist .btn { padding: 13px 14px; font-size: 16px; text-align: left; }

button, .btn, .htile, .brush { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }

/* ---------- celular: retrato (<=820px) e paisagem baixa ---------- */
@media (max-width: 820px), (max-height: 520px) and (orientation: landscape) {
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body { height: 100dvh; padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
  .topbar { padding: max(6px, env(safe-area-inset-top)) 10px 6px; gap: 8px; flex-wrap: nowrap; }
  .brand h1 { font-size: 17px; white-space: nowrap; }
  .brand svg { width: 24px; height: 24px; }
  .nav-desktop { display: none; }
  .nav-mobile { display: flex; gap: 8px; }
  .nav-mobile .btn { min-height: 40px; padding: 7px 12px; font-size: 14px; }
  .layout { flex: 1; min-height: 0; display: flex; padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); gap: 0; }
  .stage { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 5px; min-width: 0; }
  .board-wrap { flex: 1; min-height: 0; border-radius: 12px; }
  .hand { min-height: 0; padding: 4px 8px 5px; gap: 3px; border-radius: 10px; }
  .hand .who { font-size: 11px; }
  .hand .tiles { flex-wrap: nowrap; overflow-x: auto; padding: 2px 2px 4px; scrollbar-width: thin; -webkit-overflow-scrolling: touch; }
  .htile { width: 50px; height: 50px; flex: none; }
  .htile svg { width: 100%; height: 100%; }
  .htile .flip { width: 24px; height: 24px; line-height: 22px; font-size: 14px; }
  .clock { font-size: 16px; min-width: 62px; padding: 4px 7px; }
  .chip { font-size: 12px; padding: 4px 10px; }
  .brush { width: 28px; height: 28px; }
  .brushbar { gap: 5px; padding: 3px 5px; }
  .infobar { font-size: 12px; max-width: 100%; }
  .floatbar .btn { min-height: 42px; font-size: 14px; }

  /* detalhes viram uma gaveta que sobe de baixo */
  .side {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; max-height: 80dvh; overflow-y: auto;
    background: var(--bg2); border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; padding: 10px 10px calc(14px + env(safe-area-inset-bottom));
    transform: translateY(110%); transition: transform .25s ease; box-shadow: 0 -12px 40px rgba(0,0,0,.55); gap: 8px;
  }
  .side.open { transform: none; }
  .side-backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 35; }
  .side-backdrop[hidden] { display: none; }
  .sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 2px 4px; }
  .card.history { min-height: 0; max-height: 240px; flex: none; }
  #log { max-height: 190px; }

  /* diálogos como "bottom sheet" */
  dialog {
    margin: auto 0 0; width: 100%; max-width: 100%; max-height: 92dvh; overflow-y: auto;
    border-radius: 16px 16px 0 0; padding: 16px 16px calc(18px + env(safe-area-inset-bottom));
  }
  dialog.wide { max-width: 100%; }
  dialog#joinDialog, dialog#lobbyDialog, dialog#endDialog { margin: auto; width: calc(100% - 24px); border-radius: 16px; }
  .piece-list { grid-template-columns: 1fr; }
  .roomcode { font-size: 34px; letter-spacing: 6px; }
  menu { flex-wrap: wrap; }
  menu .btn { flex: 1 1 auto; min-height: 44px; }
  label.radio, label.check { padding: 7px 0; }
  .grid2 { grid-template-columns: 1fr; }
}

/* paisagem de celular: mãos ao lado do tabuleiro */
@media (max-height: 520px) and (orientation: landscape) {
  .topbar { padding: max(3px, env(safe-area-inset-top)) 10px 3px; }
  .nav-mobile .btn { min-height: 34px; padding: 4px 10px; }
  .stage { display: grid; grid-template-columns: 1fr minmax(200px, 250px); grid-template-rows: auto 1fr auto; gap: 6px; }
  .board-wrap { grid-column: 1; grid-row: 1 / 4; }
  .hand-top { grid-column: 2; grid-row: 1; }
  .hand-bottom { grid-column: 2; grid-row: 3; }
  .hand .tiles { flex-wrap: wrap; max-height: 112px; overflow-y: auto; overflow-x: hidden; }
  .htile { width: 44px; height: 44px; }
  .side { max-height: 92dvh; }
}

/* no desktop o painel lateral já mostra a peça; a barra sobre o tabuleiro só aparece no celular */
@media (min-width: 821px) and (min-height: 521px) {
  .infobar { display: none !important; }
}

@media (max-width: 820px), (max-height: 520px) and (orientation: landscape) {
  dialog menu { position: sticky; bottom: calc(-1px - env(safe-area-inset-bottom)); background: var(--panel); padding: 10px 0 2px; margin-top: 8px; }
}

.vol { display: flex; align-items: center; gap: 10px; padding: 8px 0; }
.vol input[type=range] { flex: 1; accent-color: var(--accent); height: 28px; }
#btnSound.off, #btnSoundM.off { opacity: .6; }

.premv { fill: rgba(255,140,0,.2); stroke: #ff8c00; stroke-width: 3; stroke-dasharray: 6 4; stroke-linejoin: round; pointer-events: none; }
.premove-cancel { border-color: #ff8c00 !important; color: #ffb14a; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
