/* ── TOKENS ── */
:root {
  --bg: #F4F5F7;
  --surface: #FFFFFF;
  --surface2: #F9FAFB;
  --border: #E5E7EB;
  --border2: #D1D5DB;
  --text: #111827;
  --text2: #4B5563;
  --text3: #9CA3AF;
  --blue: #1D4ED8;
  --blue-bg: #EFF6FF;
  --blue-border: #BFDBFE;
  --green: #047857;
  --green-bg: #ECFDF5;
  --amber: #B45309;
  --amber-bg: #FFFBEB;
  --red: #B91C1C;
  --red-bg: #FEF2F2;
  --sans: 'IBM Plex Sans', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', 'Courier New', monospace;
  --r: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
}

/* ── DARK MODE TOKENS ── */
[data-theme="dark"] {
  --bg: #111318;
  --surface: #1C1F26;
  --surface2: #242730;
  --border: #2E3340;
  --border2: #3A3F4E;
  --text: #F0F2F5;
  --text2: #9AA3B2;
  --text3: #5A6172;
  --blue: #4F83F0;
  --blue-bg: #1A2540;
  --blue-border: #2D4080;
  --green: #34A87C;
  --green-bg: #0F2820;
  --amber: #D4933A;
  --amber-bg: #2A1F0A;
  --red: #E05252;
  --red-bg: #2A1010;
  --shadow: 0 1px 3px rgba(0,0,0,.3), 0 1px 2px rgba(0,0,0,.2);
}

/* ── RESET ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body { background: var(--bg); color: var(--text); font-family: var(--sans); min-height: 100vh; transition: background .2s, color .2s; }
button { font-family: inherit; cursor: pointer; border: none; background: none; -webkit-tap-highlight-color: transparent; }
input { font-family: var(--mono); border: none; outline: none; background: none; width: 100%; }

/* ── LAYOUT ── */
#app { max-width: 480px; margin: 0 auto; padding: 0 0 80px; }

header {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
header .title-wrap { display: flex; align-items: baseline; gap: 10px; }
header h1 { font-family: var(--mono); font-size: 14px; font-weight: 600; letter-spacing: .03em; color: var(--text); }
header span { font-size: 11px; color: var(--text3); letter-spacing: .04em; text-transform: uppercase; }
#guide-btn {
  font-size: 12px; font-weight: 500; padding: 6px 12px; border-radius: 5px;
  border: 1px solid var(--border2); color: var(--text2); background: var(--surface2);
  display: flex; align-items: center; gap: 5px; transition: all .12s; flex-shrink: 0;
}
#guide-btn:hover { border-color: var(--blue); color: var(--blue); }
#guide-btn .qmark { display: inline-flex; align-items: center; justify-content: center; width: 15px; height: 15px; border-radius: 50%; background: var(--text3); color: #fff; font-size: 10px; font-weight: 600; }
#guide-btn:hover .qmark { background: var(--blue); }

#theme-toggle {
  width: 32px; height: 32px; border-radius: 5px; flex-shrink: 0;
  border: 1px solid var(--border2); color: var(--text2); background: var(--surface2);
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; transition: all .12s;
}
#theme-toggle:hover { border-color: var(--border2); color: var(--text); }
.header-actions { display: flex; align-items: center; gap: 6px; }

/* ── GUIDE OVERLAY ── */
#guide-overlay {
  position: fixed; inset: 0; z-index: 100; background: rgba(17,24,39,.55);
  display: none; align-items: center; justify-content: center; padding: 16px;
  animation: fadeBg .15s ease;
}
#guide-overlay.open { display: flex; }
@keyframes fadeBg { from { opacity: 0; } to { opacity: 1; } }
#guide-card {
  background: var(--surface); width: 100%; max-width: 460px; max-height: 88vh;
  border-radius: 12px; display: flex; flex-direction: column;
  box-shadow: 0 20px 50px rgba(0,0,0,.3); animation: cardUp .2s ease;
}
@keyframes cardUp { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } }
.guide-top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.guide-top-title { font-family: var(--mono); font-size: 15px; font-weight: 600; color: var(--text); }
#guide-close { width: 32px; height: 32px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface2); color: var(--text2); font-size: 15px; display: flex; align-items: center; justify-content: center; transition: all .12s; }
#guide-close:hover { border-color: var(--border2); color: var(--text); }
.guide-content { padding: 16px 18px 24px; overflow-y: auto; }
.guide-intro { font-size: 13px; line-height: 1.55; color: var(--text2); margin-bottom: 20px; }
.guide-section { margin-bottom: 18px; }
.guide-section:last-child { margin-bottom: 0; }
.guide-section-title { font-size: 10px; font-weight: 600; letter-spacing: .1em; text-transform: uppercase; color: var(--blue); margin-bottom: 8px; padding-bottom: 5px; border-bottom: 1px solid var(--border); }
.guide-def { font-size: 13px; line-height: 1.5; color: var(--text2); margin-bottom: 9px; }
.guide-def:last-child { margin-bottom: 0; }
.guide-term { font-weight: 600; color: var(--text); }
.guide-mono { font-family: var(--mono); font-size: 12px; background: var(--surface2); padding: 1px 5px; border-radius: 3px; color: var(--text); }
.guide-swatch { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 3px; vertical-align: middle; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow); margin: 12px 12px 0;
}
.card-header {
  padding: 10px 14px 0; font-size: 10px; font-weight: 600; letter-spacing: .1em;
  text-transform: uppercase; color: var(--text3);
}

/* ── CONTEXT CONTROLS ── */
#context { padding: 10px 14px 14px; display: grid; gap: 8px; }
.ctx-row { display: flex; align-items: center; gap: 8px; min-height: 32px; }
.ctx-label { font-size: 12px; color: var(--text2); width: 72px; flex-shrink: 0; }
.btn-group { display: flex; gap: 4px; flex-wrap: wrap; }
.seg {
  font-family: var(--mono); font-size: 13px; font-weight: 500;
  padding: 4px 10px; border-radius: 4px; border: 1px solid var(--border2);
  background: var(--surface2); color: var(--text2);
  transition: all .12s; min-width: 34px; text-align: center;
}
.seg:hover { border-color: var(--blue); color: var(--blue); }
.seg.on { background: var(--blue); border-color: var(--blue); color: #fff; }
.seg:disabled { opacity: .35; pointer-events: none; }

.score-picker { display: flex; align-items: center; gap: 6px; }
.score-picker button { width: 28px; height: 28px; border-radius: 4px; border: 1px solid var(--border2); background: var(--surface2); color: var(--text2); font-size: 15px; display: flex; align-items: center; justify-content: center; transition: all .12s; }
.score-picker button:hover { border-color: var(--blue); color: var(--blue); }
.score-val { font-family: var(--mono); font-size: 16px; font-weight: 600; min-width: 28px; text-align: center; }
.none-btn { font-size: 11px; padding: 4px 8px; border-radius: 4px; border: 1px solid var(--border2); background: var(--surface2); color: var(--text2); transition: all .12s; }
.none-btn.on { background: var(--surface2); border-color: var(--text3); color: var(--text2); text-decoration: line-through; opacity: .6; }
.beat-na { font-size: 12px; color: var(--text3); font-style: italic; }

/* ── DICE ── */
#dice-panel { padding: 14px; }
.dice-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.dice-row { display: flex; gap: 8px; justify-content: center; }
.die-wrap { display: flex; flex-direction: column; align-items: center; gap: 5px; }

.die {
  width: 54px; height: 54px; border-radius: 8px; cursor: pointer;
  background: var(--surface); border: 1.5px solid var(--border2);
  display: grid; grid-template-columns: repeat(3,1fr); grid-template-rows: repeat(3,1fr);
  gap: 1px; padding: 7px; transition: border-color .12s, background .12s;
  box-shadow: 0 1px 3px rgba(0,0,0,.1);
  -webkit-tap-highlight-color: transparent; user-select: none;
}
.die:hover { border-color: var(--border2); }
.die .pip { display: flex; align-items: center; justify-content: center; }
.die .pip::after { content:''; display:block; width:9px; height:9px; border-radius:50%; background:var(--text); opacity:0; }
.die .pip.on::after { opacity:1; }

/* ── NUMERIC INPUT ── */

#numeric-wrap { margin-top: 10px; position: relative; }
#numeric-input-field {
  width: 100%; padding: 8px 12px; border-radius: var(--r);
  border: 1.5px solid var(--border2); background: var(--surface2);
  font-family: var(--mono); font-size: 15px; color: var(--text);
  transition: border-color .12s;
}
#numeric-input-field:focus { border-color: var(--blue); background: #fff; outline: none; }
#numeric-input-field::placeholder { color: var(--text3); }
#hand-dropdown {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 20;
  background: var(--surface); border: 1.5px solid var(--border2); border-radius: var(--r);
  box-shadow: 0 4px 12px rgba(0,0,0,.12); overflow: hidden; max-height: 256px; overflow-y: auto;
}
.hand-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 12px; cursor: pointer; border-bottom: 1px solid var(--border);
  transition: background .1s;
}
.hand-row:last-child { border-bottom: none; }
.hand-row:hover, .hand-row.focused { background: var(--blue-bg); }
.hand-dice { font-family: var(--mono); font-size: 14px; font-weight: 500; color: var(--text); letter-spacing: .04em; }
.hand-meta { font-size: 12px; color: var(--text2); text-align: right; }
.hand-score { font-family: var(--mono); font-weight: 600; color: var(--text); }
.no-results { padding: 12px; text-align: center; font-size: 13px; color: var(--text3); }

/* ── ANALYSIS ── */
#analysis { padding: 14px 14px 16px; display: flex; flex-direction: column; gap: 12px; }

.hand-label { font-size: 13px; color: var(--text2); }
.hand-label strong { font-family: var(--mono); font-weight: 600; color: var(--text); }
.hand-status { font-size: 12px; font-weight: 500; margin-left: 6px; }
.hand-status.ahead { color: var(--green); }
.hand-status.tied  { color: var(--amber); }
.hand-status.behind{ color: var(--red); }

/* Recommendation card — primary action */
.rec-card {
  background: var(--blue-bg); border: 1px solid var(--blue-border);
  border-radius: var(--r); padding: 12px 14px;
}
.rec-card-label {
  font-size: 10px; font-weight: 600; letter-spacing: .1em; text-transform: uppercase;
  color: var(--blue); margin-bottom: 6px; opacity: .7;
}
.rec-action {
  font-family: var(--mono); font-size: 15px; font-weight: 600; color: var(--blue);
  line-height: 1.4;
}
.rec-dice-keep  { color: var(--blue); }
.rec-dice-reroll { color: var(--text3); text-decoration: line-through; font-weight: 400; }

/* P(safe) row — supporting stat */
.psafe-row { display: flex; align-items: flex-end; gap: 10px; }
.psafe-num {
  font-family: var(--mono); font-size: 40px; font-weight: 600; line-height: 1;
  letter-spacing: -.02em; flex-shrink: 0;
}
.psafe-num.high { color: var(--green); }
.psafe-num.mid  { color: var(--amber); }
.psafe-num.low  { color: var(--red); }
.psafe-meta { padding-bottom: 4px; }
.psafe-meta-label { font-size: 10px; letter-spacing: .1em; text-transform: uppercase; color: var(--text3); }
.psafe-meta-base  { font-size: 11px; color: var(--text3); margin-top: 2px; }
.psafe-delta { font-family: var(--mono); font-size: 11px; font-weight: 500; }
.psafe-delta.pos { color: var(--green); }
.psafe-delta.neg { color: var(--red); }
.psafe-delta.neu { color: var(--text3); }

.pbar-wrap { height: 4px; background: var(--border); border-radius: 2px; overflow: hidden; }
.pbar-fill { height: 100%; border-radius: 2px; transition: width .4s ease; }
.pbar-fill.high { background: var(--green); }
.pbar-fill.mid  { background: var(--amber); }
.pbar-fill.low  { background: var(--red); }

/* ── ACCORDION SECTIONS ── */
.accordion { margin: 12px 12px 0; border-radius: var(--r); border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow); overflow: hidden; }
.acc-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 14px; cursor: pointer; user-select: none;
  -webkit-tap-highlight-color: transparent; transition: background .12s;
}
.acc-header:hover { background: var(--surface2); }
.acc-title { font-size: 11px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--text2); }
.acc-arrow { font-size: 11px; color: var(--text3); transition: transform .2s; }
.acc-arrow.open { transform: rotate(180deg); }
.acc-body { display: none; border-top: 1px solid var(--border); }
.acc-body.open { display: block; }

/* ── DISTRIBUTION CHART ── */
#dist-body { padding: 12px 14px; }
.dist-row { display: flex; align-items: center; gap: 8px; margin-bottom: 5px; }
.dist-score { font-family: var(--mono); font-size: 11px; font-weight: 500; width: 24px; text-align: right; flex-shrink: 0; color: var(--text2); }
.dist-bar-wrap { flex: 1; height: 14px; background: var(--surface2); border-radius: 2px; overflow: hidden; }
.dist-bar { height: 100%; background: var(--blue); border-radius: 2px; min-width: 1px; transition: width .4s ease; opacity: .7; }
.dist-pct { font-family: var(--mono); font-size: 11px; width: 38px; text-align: right; flex-shrink: 0; color: var(--text2); }

/* ── ALTERNATIVES LIST ── */
#alt-body { padding: 8px 14px 12px; }
.alt-row { display: flex; align-items: center; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--border); }
.alt-row:last-child { border-bottom: none; }
.alt-row.optimal .alt-lock { color: var(--blue); font-weight: 600; }
.alt-dot { width: 6px; height: 6px; border-radius: 50%; flex-shrink: 0; }
.alt-dot.optimal { background: var(--blue); }
.alt-dot.other { background: var(--border2); }
.alt-lock { font-family: var(--mono); font-size: 12px; color: var(--text2); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.alt-bar-wrap { width: 72px; height: 6px; background: var(--surface2); border-radius: 3px; overflow: hidden; flex-shrink: 0; }
.alt-bar { height: 100%; border-radius: 3px; }
.alt-bar.optimal { background: var(--blue); }
.alt-bar.other { background: var(--border2); }
.alt-pct { font-family: var(--mono); font-size: 12px; font-weight: 500; width: 38px; text-align: right; flex-shrink: 0; color: var(--text); }

/* ── ABOUT / SEO CONTENT ── */
.about-section {
  margin: 12px 12px 0; padding: 18px 16px 20px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow);
}
.about-title {
  font-family: var(--sans); font-size: 13px; font-weight: 600;
  color: var(--text); margin-bottom: 10px;
}
.about-subtitle {
  font-family: var(--sans); font-size: 12px; font-weight: 600;
  color: var(--text2); margin: 12px 0 6px;
}
.about-section p {
  font-size: 12px; line-height: 1.6; color: var(--text3); margin-bottom: 6px;
}
.about-section p:last-child { margin-bottom: 0; }
.about-section strong { color: var(--text2); font-weight: 600; }
