:root,
[data-theme="dark"] {
  --bg: #0e0e10;
  --panel: #18181b;
  --raised: #222227;
  --border: #323239;
  --text: #efeff1;
  --muted: #a8a8b3;
  --accent: #7c4dff;
  --accent-fg: #b794ff;
  --on-accent: #ffffff;
  --danger: #ff8080;
  --sub-bg: #3a2a66;
  --sub-fg: #d6c4ff;
  --win-bg: #241a3d;
  --highlight: rgba(124, 77, 255, 0.13);
  --ok: #3fbf6f;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #f2f2f5;
  --panel: #ffffff;
  --raised: #f6f6f8;
  --border: #dcdce3;
  --text: #18181b;
  --muted: #5c5c66;
  --accent: #6d3fe0;
  --accent-fg: #6d3fe0;
  --on-accent: #ffffff;
  --danger: #c62828;
  --sub-bg: #ece4ff;
  --sub-fg: #4b23b0;
  --win-bg: #f3eeff;
  --highlight: rgba(109, 63, 224, 0.08);
  --ok: #23874a;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.4;
}

h2 { margin: 0; font-size: 16px; font-weight: 600; }
code { font-family: ui-monospace, Consolas, monospace; }
.muted { color: var(--muted); font-weight: 500; }
.small { font-size: 12px; }
.center { text-align: center; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 40px; padding: 0 14px;
  border-radius: 8px; border: 1px solid var(--border);
  background: var(--raised); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer;
}
.btn:hover { filter: brightness(1.12); }
.btn:focus-visible, .input:focus-visible, .segmented button:focus-visible, input[type=range]:focus-visible, .icon-btn:focus-visible {
  outline: 2px solid var(--accent-fg); outline-offset: 2px;
}
.btn-primary { background: var(--accent); border-color: transparent; color: var(--on-accent); font-weight: 600; }
.btn-ghost { background: transparent; }
.btn-danger { background: transparent; color: var(--danger); }
.btn-sm { height: 36px; padding: 0 12px; font-size: 13px; }
.btn-lg { height: 52px; border-radius: 10px; font-size: 16px; font-weight: 700; width: 100%; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; filter: none; }

.input {
  height: 40px; width: 100%; padding: 0 12px;
  border-radius: 8px; border: 1px solid var(--border);
  background: var(--raised); color: var(--text); font: inherit;
}
input[type=range] { width: 100%; accent-color: var(--accent); }

.field { display: flex; flex-direction: column; gap: 6px; }
.field.grow { flex: 1; min-height: 0; }
.label { font-size: 12px; color: var(--muted); }
.hint { margin: 0; font-size: 13px; color: var(--muted); }
.value { font-weight: 600; }
.row-between { display: flex; justify-content: space-between; align-items: baseline; }

.segmented {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px; padding: 4px;
  border-radius: 10px; background: var(--raised); border: 1px solid var(--border);
}
.segmented button {
  height: 40px; border-radius: 7px; border: none; background: transparent; color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; padding: 0 12px;
}
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.segmented-sm { padding: 3px; border-radius: 9px; }
.segmented-sm button { height: 34px; font-size: 13px; border-radius: 6px; }

.badge-sub {
  display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 5px; border-radius: 4px;
  background: var(--sub-bg); color: var(--sub-fg); letter-spacing: 0.02em; vertical-align: 1px;
}

/* ---------- Login ---------- */
.login-view { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card {
  width: 100%; max-width: 440px; display: flex; flex-direction: column; gap: 20px; padding: 32px;
  border-radius: 16px; background: var(--panel); border: 1px solid var(--border);
}
.login-card p { margin: 0; line-height: 1.5; }
.scope-list {
  list-style: none; margin: 0; padding: 12px; display: flex; flex-direction: column; gap: 8px;
  border-radius: 10px; background: var(--raised); border: 1px solid var(--border);
}
.scope-list li { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.scope-list code { font-size: 12px; color: var(--muted); }
.error-box { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--danger); color: var(--danger); }

/* ---------- App layout ---------- */
.app-view { min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.brand svg { color: var(--accent-fg); }
.brand-lg { font-size: 22px; gap: 10px; }
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.user { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.avatar-sm { width: 32px; height: 32px; border-radius: 50%; }
[data-theme="dark"] .icon-moon, [data-theme="light"] .icon-sun { display: none; }

.columns {
  --col1: 1; --col2: 2; --col3: 1;
  flex: 1; display: flex; flex-wrap: nowrap; gap: 0; padding: 16px; align-items: stretch;
}
.panel {
  height: calc(100vh - 97px); min-height: 560px;
  display: flex; flex-direction: column; gap: 14px; padding: 16px;
  border-radius: 12px; background: var(--panel); border: 1px solid var(--border);
  min-width: 240px;
}
#entries-panel { flex: var(--col1) 1 0px; }
#settings-panel, #winner-panel { flex: var(--col2) 1 0px; gap: 22px; }
#chat-panel { flex: var(--col3) 1 0px; }
.panel-chat { padding: 0; gap: 0; }

/* Drag handles between the columns */
.splitter { flex: 0 0 16px; position: relative; cursor: col-resize; touch-action: none; }
.splitter::after {
  content: ""; position: absolute; top: 12px; bottom: 12px; left: 7px; width: 2px; border-radius: 1px;
  background: transparent; transition: background 0.15s;
}
.splitter:hover::after, .splitter.is-dragging::after { background: var(--border); }
.splitter.is-dragging::after { background: var(--accent); }
.splitter:focus-visible { outline: none; }
.splitter:focus-visible::after { background: var(--accent-fg); }
body.is-resizing { cursor: col-resize; user-select: none; }
body.is-resizing iframe { pointer-events: none; }
.panel-winner { border-color: var(--accent); gap: 18px !important; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.scroll { overflow-y: auto; min-height: 0; }

@media (max-width: 760px) {
  .columns { flex-wrap: wrap; gap: 16px; }
  .splitter { display: none; }
  .panel { height: 600px; min-height: 0; min-width: 0; }
  #entries-panel, #settings-panel, #winner-panel, #chat-panel { flex: 1 1 100%; }
}

/* ---------- Entries ---------- */
.entry-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.entry {
  display: flex; align-items: center; gap: 10px; padding: 6px 6px 6px 12px;
  border-radius: 8px; background: var(--raised); border: 1px solid transparent;
}
.entry.is-winner { background: var(--win-bg); border-color: var(--accent); }
.entry-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-ago { margin-left: auto; font-size: 12px; color: var(--muted); white-space: nowrap; }
.entry.is-stale .entry-name { color: var(--muted); }
.icon-btn {
  width: 32px; height: 32px; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  border-radius: 6px; border: none; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--border); color: var(--text); }
.empty { margin: 24px 0; }

/* ---------- Settings ---------- */
.status-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px;
  border-radius: 10px; background: var(--raised); border: 1px solid var(--border);
}
.status { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.is-open { background: var(--ok); }
#open-btn.is-open { background: transparent; color: var(--text); border-color: var(--border); }
#pick-btn { margin-top: auto; }

/* ---------- Winner ---------- */
.winner-card {
  display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 20px 16px;
  border-radius: 10px; background: var(--win-bg); text-align: center;
}
.avatar-lg {
  position: relative; width: 88px; height: 88px; border-radius: 50%; overflow: hidden;
  background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 36px; font-weight: 700;
}
.avatar-lg img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.winner-name-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: center; }
.winner-name { font-size: 26px; font-weight: 700; word-break: break-word; }
.winner-name-row .badge-sub { font-size: 12px; padding: 3px 8px; }
.facts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; width: 100%; margin-top: 6px; }
.fact { padding: 10px; border-radius: 8px; background: var(--panel); border: 1px solid var(--border); }
.fact-value { font-weight: 600; margin-top: 2px; }
.winner-messages {
  list-style: none; margin: 0; padding: 8px; display: flex; flex-direction: column; gap: 6px; flex: 1;
  border-radius: 10px; background: var(--raised); border: 1px solid var(--border);
}
.winner-messages li { display: flex; gap: 10px; line-height: 1.45; word-break: break-word; }
.winner-messages .time { padding-top: 1px; }
.winner-messages .none { color: var(--muted); }

/* ---------- Chat ---------- */
.chat-list { list-style: none; margin: 0; padding: 10px 8px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.chat-msg { padding: 5px 8px; border-radius: 6px; line-height: 1.45; word-break: break-word; }
.chat-msg.is-entry { background: var(--highlight); }
.time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.chat-msg .time { margin-right: 6px; }
.chat-msg .badge-sub { margin-right: 4px; }
.chat-name { font-weight: 700; }
.chat-sep { color: var(--muted); }
.emote { height: 1.6em; vertical-align: middle; margin: -0.3em 0; }
.chat-empty { color: var(--muted); text-align: center; padding: 24px; }
.embed-wrap { flex: 1; display: flex; }
.embed-wrap iframe { flex: 1; width: 100%; border: 0; border-radius: 0 0 12px 12px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  max-width: min(560px, calc(100vw - 32px)); padding: 12px 16px; border-radius: 10px;
  background: var(--text); color: var(--bg); font-weight: 500; box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  z-index: 10;
}
