:root {
  --bg: #0f172a;
  --fg: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22c55e;
  --danger: #ef4444;
  --warn: #f59e0b;
  --panel: #111827;
  --tile: #1f2937;
  --tile-dim: #0b1220;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overflow-x: hidden; }
body { font-family: system-ui, sans-serif; background: var(--bg); color: var(--fg); }

#app { max-width: 1100px; margin: 0 auto; padding: 16px; }
header { display: flex; align-items: baseline; gap: 16px; margin-bottom: 12px; }
h1 { margin: 0; font-size: 24px; }
.topic { color: var(--muted); }
.conn-head { margin-left: auto; display: flex; align-items: center; }
.conn-dot { width: 10px; height: 10px; border-radius: 999px; display: inline-block; border: 2px solid #111827; box-shadow: 0 0 0 2px rgba(0,0,0,0.2) inset; }
.conn-dot.online { background: #16a34a; }
.conn-dot.offline { background: #b91c1c; }
.conn-dot.reconnecting { background: #f59e0b; animation: pulse 1s infinite; }
@keyframes pulse { 0% { opacity: 0.6; } 50% { opacity: 1; } 100% { opacity: 0.6; } }

.panel { background: var(--panel); border: 1px solid #1f2937; border-radius: 8px; padding: 16px; }
.row { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.row.center { justify-content: center; }
.row .field { display: inline-flex; align-items: center; gap: 8px; }
label { width: 100px; color: var(--muted); }
input[type="text"] { flex: 1; padding: 10px 12px; border-radius: 6px; border: 1px solid #334155; background: #0b1220; color: var(--fg); }
input.code { width: 120px; text-transform: uppercase; }
/* Styled selects */
select { flex: 0 0 auto; min-width: 140px; padding: 10px 32px 10px 12px; border-radius: 6px; border: 1px solid #334155; background: #0b1220; color: var(--fg); cursor: pointer;
  appearance: none; -webkit-appearance: none; -moz-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 20 20' fill='currentColor'><path d='M5.23 7.21a.75.75 0 011.06.02L10 11.06l3.71-3.83a.75.75 0 111.08 1.04l-4.24 4.38a.75.75 0 01-1.08 0L5.21 8.27a.75.75 0 01.02-1.06z'/></svg>");
  background-repeat: no-repeat; background-position: right 10px center; background-size: 12px; }
select:hover { border-color: #3b82f6; }
select:focus { outline: none; border-color: #3b82f6; box-shadow: 0 0 0 3px rgba(59,130,246,0.25); }
select:disabled { opacity: 0.6; cursor: not-allowed; }
select::-ms-expand { display: none; }
button { background: #0ea5e9; border: none; color: white; padding: 10px 14px; border-radius: 6px; cursor: pointer; }
button:disabled { opacity: 0.6; cursor: not-allowed; }
.error { color: var(--danger); min-height: 20px; }
.hidden { display: none; }
/* Ensure hidden works against specific components with higher specificity */
#game.hidden { display: none !important; }
.conn.hidden { display: none !important; }

#game { display: grid; grid-template-columns: 280px 1fr; gap: 16px; }
.sidebar { background: var(--panel); border: 1px solid #1f2937; border-radius: 8px; padding: 12px; display: flex; flex-direction: column; gap: 12px; }
.players ul, .log ul { padding-left: 18px; margin: 6px 0; }
.players li { margin: 4px 0; }
.players li.turn { background: rgba(34,197,94,0.15); border-left: 3px solid var(--accent); padding-left: 6px; border-radius: 4px; }
.status { color: var(--muted); }
.conn { display: flex; align-items: center; justify-content: space-between; gap: 8px; background: rgba(59,130,246,0.07); border: 1px solid #334155; border-radius: 6px; padding: 8px; }
.conn #connText { color: var(--muted); }
.conn button { background: transparent; color: var(--fg); border: 1px solid #334155; padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 12px; }
.conn button:hover { border-color: #3b82f6; }
.log ul { max-height: 220px; overflow-y: auto; }
.secretbox { background: rgba(255,255,255,0.02); border: 1px dashed #334155; border-radius: 6px; padding: 8px; }
.secret-display { flex: 1; font-weight: 600; }
.secret-hidden { color: var(--muted); }
.pill { display:inline-flex; align-items:center; justify-content:center; width:22px; height:22px; border-radius:999px; border:1px solid #475569; background:#334155; color:#fff; font-weight:700; margin-right:8px; font-size:12px; }
.ask input { width: 100%; }
.answer .q { margin: 6px 0 8px; }
.answer .yes { background: var(--accent); }
.answer .no { background: var(--danger); }
.answer .maybe { background: var(--warn); color: #111; }
.guess { margin-top: auto; }

.board { position: relative; background: var(--panel); border: 1px solid #1f2937; border-radius: 8px; padding: 12px; }
.board { overflow: hidden; }
.grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
#boardGrid { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.tile { background: var(--tile); border: 1px solid #334155; border-radius: 8px; padding: 16px; display: flex; align-items: center; justify-content: center; text-align: center; min-height: 70px; transition: transform 0.05s ease, background 0.2s; user-select: none; cursor: pointer; max-width: 100%; overflow-wrap: anywhere; word-break: break-word; }
.tile.eliminated { background: var(--tile-dim); opacity: 0.5; }
.tile.selected { outline: 2px solid var(--accent); }
.tile.locked { outline: 2px dashed var(--danger); cursor: not-allowed; }
.hint { margin-top: 10px; color: var(--muted); font-size: 12px; }

.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; }
.overlay.hidden { display: none; }
#pickOverlay { position: fixed; inset: 0; z-index: 3000; }
#resultOverlay { position: fixed; inset: 0; z-index: 3500; }
.modal { background: #0b1220; border: 1px solid #334155; border-radius: 8px; padding: 16px; width: min(600px, 92vw); max-height: 90vh; display: flex; flex-direction: column; box-sizing: border-box; }
.modal h3 { margin-top: 0; }
.modal .grid { flex: 1 1 auto; overflow: auto; }
#pickOverlay .modal .actions { position: static; margin-top: 8px; background: rgba(11,18,32,0.95); border-top: 1px solid #334155; padding-top: 8px; }
#pickGrid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
#pickGrid .tile { min-height: 64px; }
.codebar { gap: 10px; }
.codepill { background: #0b1220; border: 1px solid #334155; padding: 6px 10px; border-radius: 999px; font-size: 12px; color: var(--fg); }

.room { font-weight: 600; }
.linkbtn { background: transparent; color: var(--fg); border: 1px solid #334155; padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 12px; }
.linkbtn:hover { border-color: #3b82f6; }

/* Toast notifications */
.toast { position: fixed; right: 16px; bottom: 16px; z-index: 1000; display: flex; flex-direction: column; gap: 8px; }
.toast .item { background: #0b1220; border: 1px solid #334155; color: var(--fg); padding: 10px 12px; border-radius: 8px; box-shadow: 0 10px 20px rgba(0,0,0,0.25); animation: slidein 200ms ease-out; }
.toast .item.success { border-color: #15803d; background: rgba(34,197,94,0.1); }
.toast .item.error { border-color: #b91c1c; background: rgba(239,68,68,0.1); }
.toast .item.info { border-color: #1d4ed8; background: rgba(59,130,246,0.1); }
@keyframes slidein { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* Result overlay styling */
.modal.result h2 { margin: 0 0 8px; font-size: 24px; }
.modal.result p { margin: 0 0 12px; color: var(--muted); }
.modal.result.win h2 { color: var(--accent); }
.modal.result.lose h2 { color: var(--danger); }
/* Mode bar */
.modebar { display:flex; flex-direction: column; align-items:flex-start; gap:6px; }
.modebar .label { width:auto; color: var(--muted); font-size: 12px; }
.modebar .seg { display:inline-flex; background:#0b1220; border:1px solid #334155; border-radius:8px; overflow:hidden; }
.modebar .seg button { background:transparent; border:none; color:var(--fg); padding:8px 10px; font-size:12px; cursor:pointer; }
.modebar .seg button.active { background:#1f2937; }

/* Ensure secret boxes stack vertically and span the sidebar */
.secretbox { width: 100%; }

/* Responsive layout */
@media (max-width: 800px) {
  #app { padding: 8px; }
  #game { grid-template-columns: 1fr; }
  .grid { gap: 8px; }
  .tile { min-height: 56px; padding: 12px; font-size: clamp(11px, 3.6vw, 14px); }
  .players ul, .log ul { max-height: 150px; overflow-y: auto; }
  select { min-width: 120px; padding: 10px 28px 10px 10px; }
  .row-choose { flex-direction: column; align-items: flex-start; gap: 6px; }
  #pickGrid { grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); }
  #pickGrid .tile { min-height: 56px; padding: 10px; }
  /* On small screens, make overlay fixed so it can fully cover viewport and allow scrolling inside */
  #pickOverlay { position: fixed; }
}

/* Host overlay (regen) compact row layout */
.host-regen { margin-bottom: 8px; }
.host-regen .row-choose { flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px; }
.host-regen .field { display: flex; flex-direction: column; width: auto; flex: 1 1 160px; min-width: 160px; }
.host-regen .field label { display: none; }
.host-regen .field select { width: 100%; min-width: 0; }
.host-regen hr { border: none; border-top: 1px solid #334155; margin: 8px 0; }
