:root {
  --bg: #FAF9F5;
  --surface: #FFFFFF;
  --border: #E8E4DA;
  --text: #29261F;
  --muted: #857F72;
  --accent: #D97757;
  --accent-deep: #B05730;
  --accent-tint: #FBF1EC;
  --accent-border: #F3DCD1;
  --banner-bg: #F5EBDC;
  --banner-text: #8A5A2B;
  --success-bg: #EAF1E2;
  --success-text: #3F6E2A;
  --chip: #F0EDE5;
  --radius: 12px;
  --serif: 'Fraunces', Georgia, serif;
  --sans: 'Inter', system-ui, sans-serif;
  --mono: 'Spline Sans Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: 20px 18px calc(32px + env(safe-area-inset-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

h1, h2 { font-family: var(--serif); font-weight: 500; }
h1 { font-size: 24px; line-height: 1.25; }
h2 { font-size: 19px; line-height: 1.3; }

.center { text-align: center; }
.muted { color: var(--muted); font-size: 13px; }
.small { font-size: 13px; }
.spacer { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 10px; }

.appmark { text-align: center; margin-top: 10vh; }
.appmark .leaf { font-size: 30px; }
.appmark h1 { margin-top: 6px; }
.appmark .muted { margin-top: 4px; }

.header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}
.header h2 { font-size: 20px; }
.header .muted { white-space: nowrap; }

.banner {
  background: var(--banner-bg);
  color: var(--banner-text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13.5px;
  margin-bottom: 12px;
}

.scanmark {
  width: 72px;
  height: 72px;
  margin: 0 auto;
  border: 2px dashed var(--border);
  border-radius: 16px;
  font-size: 34px;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
}

.topiclist { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.topic {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  font-size: 15px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.topic .rank {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--chip);
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.topic .grip { margin-left: auto; color: var(--muted); flex-shrink: 0; }
.topic.dragging, .sortable-chosen {
  border-color: var(--accent);
  background: var(--accent-tint);
}
.sortable-ghost { opacity: 0.4; }

button.primary {
  font-family: var(--sans);
  font-size: 15px;
  font-weight: 500;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  padding: 14px;
  width: 100%;
  cursor: pointer;
}
button.primary:active { background: var(--accent-deep); }
button.primary:disabled { background: var(--border); color: var(--muted); cursor: default; }

button.quiet {
  font-family: var(--sans);
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  width: 100%;
  cursor: pointer;
}

.linkish {
  background: none;
  border: none;
  color: var(--muted);
  font-family: var(--sans);
  font-size: 13px;
  text-decoration: underline;
  cursor: pointer;
  padding: 8px;
}

input.field {
  font-family: var(--sans);
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 14px;
  width: 100%;
  outline: none;
}
input.field:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-tint); }
input.field.pin { font-family: var(--mono); letter-spacing: 6px; }
label.fieldlabel { font-size: 13px; color: var(--muted); margin-bottom: -4px; }

.bigcheck {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--success-bg);
  color: var(--success-text);
  font-size: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
}

.statcard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
}
.statcard .big { font-family: var(--mono); font-size: 30px; font-weight: 500; }
.statrow { display: flex; gap: 10px; }
.statrow .statcard { flex: 1; }

.winner {
  background: var(--success-bg);
  border-radius: var(--radius);
  padding: 16px;
  text-align: center;
  color: var(--success-text);
}
.winner .title { font-family: var(--serif); font-size: 21px; font-weight: 500; margin: 2px 0; }

.resultrow { margin-top: 12px; }
.resultrow .line { display: flex; justify-content: space-between; font-size: 14px; margin-bottom: 4px; }
.resultrow .pts { color: var(--muted); font-family: var(--mono); font-size: 13px; }
.resultrow .bar { height: 6px; border-radius: 3px; background: var(--accent-border); }
.resultrow .bar > div { height: 6px; border-radius: 3px; background: var(--accent); }

.codecard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
}
.linkstatus {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 500;
  margin-top: 6px;
}
.linkstatus .dot { width: 9px; height: 9px; border-radius: 50%; }
.linkstatus.live .dot { background: var(--success-text); }
.linkstatus.paused .dot { background: var(--banner-text); }
.linkstatus.paused { color: var(--banner-text); }

.checkline {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 15px;
}
.checkline input[type="checkbox"] { width: 19px; height: 19px; accent-color: var(--accent); flex-shrink: 0; }

.iconbtn {
  width: 42px;
  height: 42px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 18px;
  cursor: pointer;
  color: var(--text);
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(41, 38, 31, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 10;
}
.overlay .sheet {
  background: var(--surface);
  border-radius: 16px;
  padding: 24px;
  text-align: center;
  width: 100%;
  max-width: 340px;
}
.overlay .sheet #qr { display: flex; justify-content: center; margin: 16px 0; }

.historyrow {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 2px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.historyrow .muted { flex-shrink: 0; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(26px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  padding: 11px 18px;
  border-radius: 10px;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
  max-width: 90vw;
  text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
