:root{
  --tone-a: var(--accent);
  --tone-b: #176c74;
  --tone-c: #6b7280;
  --paper: rgba(255,255,255,.72);
  --glass: rgba(255,255,255,.6);
  --surface-1: rgba(255,255,255,.88);
  --surface-2: rgba(255,255,255,.62);
  --surface-3: rgba(255,255,255,.55);
  --surface-4: rgba(255,255,255,.45);
  --meter-bg: rgba(0,0,0,.06);
}

@media (prefers-color-scheme: dark){
  :root{
    --tone-b: #51c7c8;
    --paper: rgba(36,36,56,.75);
    --glass: rgba(36,36,56,.6);
    --surface-1: rgba(36,36,56,.92);
    --surface-2: rgba(36,36,56,.72);
    --surface-3: rgba(36,36,56,.62);
    --surface-4: rgba(36,36,56,.52);
    --meter-bg: rgba(255,255,255,.10);
  }
}

*{box-sizing:border-box}

body{
  margin:0;
  color:var(--ink);
  background:
    radial-gradient(1000px 700px at 10% -10%, rgba(199,73,31,.20), transparent 55%),
    radial-gradient(900px 650px at 120% 10%, rgba(23,108,116,.18), transparent 60%),
    linear-gradient(180deg, var(--bg), rgba(244,240,232,.55));
  font-family:var(--font-sans);
  overflow-x:hidden;
}

.app{
  max-width:1400px;
  margin:0 auto;
  padding:8px 12px;
  display:flex;
  flex-direction:column;
  height:100vh;
  height:100dvh;
  min-height:0;
}

/* === Compact Header === */
.header-bar{
  display:flex;
  align-items:baseline;
  gap:16px;
  padding:8px 12px;
  background:var(--paper);
  border:1px solid var(--line);
  border-radius:var(--radius-md);
  box-shadow:var(--shadow-card);
  flex-shrink:0;
}

.header-left{
  display:flex;
  align-items:baseline;
  gap:10px;
  flex-shrink:0;
}

.kicker{
  font-family:var(--font-mono);
  font-size:11px;
  letter-spacing:.08em;
  text-transform:uppercase;
  color:var(--ink-secondary);
  flex-shrink:0;
}

.header-bar h1{
  margin:0;
  font-family:var(--font-serif);
  font-size:17px;
  line-height:1.1;
  white-space:nowrap;
}

.header-desc{
  margin:0;
  color:var(--ink-secondary);
  font-size:12px;
  line-height:1.4;
  flex:1;
  min-width:0;
}

/* === Main 3-Column Grid === */
.main-grid{
  margin-top:8px;
  display:grid;
  gap:8px;
  grid-template-columns:260px 1fr 240px;
  flex:1;
  min-height:0;
}

@media (max-width: 1100px){
  .main-grid{grid-template-columns:240px 1fr}
  .insight-panel{display:none}
}

@media (max-width: 720px){
  .main-grid{grid-template-columns:1fr}
  .header-bar{flex-wrap:wrap}
  .header-desc{display:none}
  .app{height:auto;min-height:100vh}
}

.panel{
  background:var(--card);
  border:1px solid var(--line);
  border-radius:var(--radius-md);
  padding:10px;
  box-shadow:var(--shadow-card);
  min-height:0;
  display:flex;
  flex-direction:column;
}

.panel h2{
  margin:0 0 6px;
  font-size:13px;
  letter-spacing:.02em;
}

/* === Controls Panel === */
.controls{overflow-y:auto}

.controls .field{margin-bottom:6px}

.field .field-row{
  display:flex;
  justify-content:space-between;
  align-items:center;
  margin-bottom:2px;
}

.field label{
  font-weight:650;
  font-size:12px;
}

.field-row output{
  font-family:var(--font-mono);
  font-size:11px;
  color:var(--ink-secondary);
}

input[type="range"]{
  width:100%;
  margin:0;
  accent-color:var(--accent);
  height:16px;
}

.actions{
  margin-top:8px;
  display:grid;
  grid-template-columns:1fr 1fr;
  gap:4px;
}

.btn{
  appearance:none;
  border:1px solid var(--line);
  border-radius:8px;
  padding:6px 8px;
  font:inherit;
  font-size:12px;
  font-weight:650;
  cursor:pointer;
  background:linear-gradient(180deg, var(--surface-1), var(--surface-2));
  color:var(--ink);
  transition:transform .12s ease, box-shadow .12s ease, border-color .12s ease;
}

.btn:hover{
  transform:translateY(-1px);
  box-shadow:var(--shadow-hover);
  border-color:var(--accent);
}

.btn:active{transform:translateY(0)}

.btn.primary{
  border-color:var(--accent-hover);
  background:linear-gradient(135deg, var(--accent), var(--accent-hover));
  color:#fff;
}

.btn.ghost{background:transparent}
.btn.subtle{background:linear-gradient(180deg, var(--accent-light), var(--surface-3))}

.status{
  margin:6px 0 0;
  padding:6px 8px;
  border-radius:8px;
  border:1px solid var(--line);
  background:linear-gradient(180deg, var(--surface-2), var(--surface-4));
  font-size:11px;
  color:var(--ink-secondary);
  border-left:3px solid var(--accent);
  line-height:1.4;
}

/* === Stage Panel === */
.stage{
  min-height:0;
  overflow:hidden;
}

/* Meters row */
.meters-row{
  display:flex;
  gap:8px;
  align-items:center;
  flex-wrap:wrap;
  margin-bottom:6px;
}

.stat-inline{
  display:flex;
  align-items:center;
  gap:6px;
  flex:1;
  min-width:160px;
}

.stat-label{
  font-size:11px;
  font-weight:650;
  color:var(--ink-secondary);
  flex-shrink:0;
  width:28px;
}

.meter-mini{
  flex:1;
  height:8px;
  border-radius:999px;
  overflow:hidden;
  background:var(--meter-bg);
  display:flex;
}

.meter-fill{height:100%;transition:width .3s ease}
.meter-fill.a{background:linear-gradient(90deg, var(--tone-a), var(--accent-hover))}
.meter-fill.b{background:linear-gradient(90deg, var(--tone-b), #0f565d)}

.stat-val{
  font-family:var(--font-mono);
  font-size:10px;
  color:var(--ink-secondary);
  white-space:nowrap;
  flex-shrink:0;
}

.compact-stats{
  min-width:auto;
  font-family:var(--font-mono);
  font-size:11px;
  color:var(--ink-secondary);
  gap:4px;
  flex:0 0 auto;
}

.dotsep{opacity:.4}

/* Chart */
.chart-wrap{
  border:1px solid var(--line);
  border-radius:10px;
  background:linear-gradient(180deg, var(--surface-2), var(--surface-4));
  padding:6px 8px 4px;
  margin-bottom:6px;
}

#chartCanvas{
  width:100%;
  height:160px;
  display:block;
}

.chart-legend{
  display:flex;
  gap:10px;
  font-size:10px;
  color:var(--ink-secondary);
  padding-top:2px;
}

.chart-legend .swatch{
  width:10px;
  height:10px;
  display:inline-block;
  border-radius:3px;
  margin-right:4px;
  vertical-align:-1px;
}
.chart-legend .swatch.a{background:var(--tone-a)}
.chart-legend .swatch.b{background:var(--tone-b)}
.chart-legend .swatch.c{background:#7c3aed}

/* Thread / Forum */
.thread-section{
  flex:1;
  min-height:0;
  display:flex;
  flex-direction:column;
}

.thread-head{
  display:flex;
  align-items:baseline;
  justify-content:space-between;
  gap:8px;
  padding:0 2px 4px;
  border-bottom:1px solid var(--line);
  margin-bottom:4px;
  flex-shrink:0;
}

.thread-title{
  margin:0;
  font-size:13px;
  font-weight:750;
}

.thread-meta{
  font-family:var(--font-mono);
  font-size:11px;
  color:var(--ink-secondary);
  white-space:nowrap;
}

.post-list{
  display:flex;
  flex-direction:column;
  gap:4px;
  overflow-y:auto;
  flex:1;
  min-height:0;
  padding-right:2px;
}

/* Scrollbar styling */
.post-list::-webkit-scrollbar{width:4px}
.post-list::-webkit-scrollbar-track{background:transparent}
.post-list::-webkit-scrollbar-thumb{background:var(--line);border-radius:4px}

.post{
  position:relative;
  border-radius:8px;
  border:1px solid var(--line);
  background:linear-gradient(180deg, var(--surface-1), var(--surface-2));
  padding:6px 8px 5px;
  overflow:hidden;
  animation:postIn .18s ease both;
  flex-shrink:0;
}

@keyframes postIn{
  from{opacity:0;transform:translateY(3px)}
  to{opacity:1;transform:translateY(0)}
}

.post::before{
  content:"";
  position:absolute;
  left:0;top:0;bottom:0;
  width:3px;
  background:var(--tone-c);
  opacity:.85;
}

.post[data-stance="A"]::before{background:var(--tone-a)}
.post[data-stance="B"]::before{background:var(--tone-b)}

.post-head{
  display:flex;
  gap:6px;
  align-items:center;
  justify-content:space-between;
  margin-bottom:3px;
}

.badge{
  display:inline-flex;
  align-items:center;
  gap:5px;
  font-family:var(--font-mono);
  font-size:10px;
  color:var(--ink-secondary);
}

.stance{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  width:16px;height:16px;
  border-radius:4px;
  font-weight:800;
  font-size:10px;
  color:#fff;
  background:var(--tone-c);
}
.post[data-stance="A"] .stance{background:var(--tone-a)}
.post[data-stance="B"] .stance{background:var(--tone-b)}

.post-body{
  color:var(--ink);
  font-size:12px;
  line-height:1.4;
  margin:0 0 4px;
}

.post-foot{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:6px;
}

.votes{
  display:flex;
  gap:4px;
  align-items:center;
}

.vote-btn{
  appearance:none;
  border:1px solid var(--line);
  background:var(--surface-3);
  color:var(--ink);
  border-radius:6px;
  padding:3px 6px;
  font:inherit;
  font-size:10px;
  cursor:pointer;
  transition:transform .12s ease,border-color .12s ease;
}

.vote-btn:hover{
  transform:translateY(-1px);
  border-color:var(--accent);
}

.vote-btn:active{transform:translateY(0)}

.score{
  font-family:var(--font-mono);
  font-size:10px;
  color:var(--ink-secondary);
}

.vis{
  font-family:var(--font-mono);
  font-size:10px;
  color:var(--ink-secondary);
  opacity:.85;
  white-space:nowrap;
}

.post.is-shadowed{opacity:.5;filter:saturate(.85)}
.post.is-shadowed .post-body{color:var(--ink-secondary)}

/* === Insight Panel === */
.insight-panel{overflow-y:auto}

.insight-text{
  margin:0 0 6px;
  color:var(--ink-secondary);
  font-size:12px;
  line-height:1.5;
}

.alert{
  border:1px solid var(--bad);
  background:linear-gradient(135deg, var(--bad-light), var(--surface-2));
  border-radius:8px;
  padding:8px 10px;
  color:var(--bad);
  font-weight:750;
  font-size:12px;
  margin:6px 0 8px;
  box-shadow:0 8px 24px rgba(166,31,18,.12);
}

.hidden{display:none}

.tips{display:grid;gap:6px;margin-bottom:8px}
.tip{
  border:1px solid var(--line);
  background:linear-gradient(180deg, var(--surface-2), var(--surface-4));
  border-radius:8px;
  padding:6px 8px;
}
.tip-h{font-weight:750;font-size:12px}
.tip-p{margin:3px 0 0;font-size:11px;color:var(--ink-secondary)}

.teach-card{
  border-top:1px dashed var(--line);
  padding-top:6px;
  color:var(--ink-secondary);
  font-size:12px;
}
.teach-card summary{
  cursor:pointer;
  font-weight:650;
  color:var(--ink);
  font-size:12px;
}
.teach-card p{
  margin:6px 0 0;
  font-size:11px;
  line-height:1.5;
}

@media (prefers-reduced-motion: reduce){
  .btn,.post{transition:none;animation:none}
  .btn:hover{transform:none}
}

/* === Dark Mode Override === */
:root[data-theme="dark"]{
  --tone-b:#aaaabc;
  --tone-c:#aaaabc;
}
@media (prefers-color-scheme: dark){
  :root:not([data-theme="light"]){
    --tone-b:#aaaabc;
    --tone-c:#aaaabc;
  }
}

/* === Animations === */
@keyframes fadeInUp{
  from{opacity:0;transform:translateY(10px)}
  to{opacity:1;transform:translateY(0)}
}
.app{animation:fadeInUp 0.3s ease-out}

@keyframes cardReveal{
  from{opacity:0;transform:translateY(8px) scale(0.98)}
  to{opacity:1;transform:translateY(0) scale(1)}
}
.panel{animation:cardReveal 0.3s ease-out both}
.panel:nth-child(2){animation-delay:0.05s}
.panel:nth-child(3){animation-delay:0.1s}

@keyframes btnPulse{
  0%,100%{box-shadow:0 2px 4px rgba(0,0,0,0.1)}
  50%{box-shadow:0 2px 12px rgba(199,73,31,0.25)}
}
.btn.primary{animation:btnPulse 3s ease-in-out infinite}

@media (prefers-reduced-motion: reduce){
  *,*::before,*::after{
    animation-duration:0.01ms!important;
    animation-iteration-count:1!important;
    transition-duration:0.01ms!important;
  }
}

/* === Loading & Feedback States === */
@keyframes spin { to { transform: rotate(360deg); } }
.btn-loading { position: relative; pointer-events: none; opacity: 0.7; }
.btn-loading::after {
  content: '';
  position: absolute;
  right: 8px; top: 50%;
  width: 14px; height: 14px;
  margin-top: -7px;
  border: 2px solid transparent;
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
.toast-msg {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #333; color: #fff; padding: 10px 20px; border-radius: 8px;
  font-size: 0.9rem; z-index: 9999; opacity: 0;
  transition: opacity 0.3s ease;
}
.toast-msg.show { opacity: 1; }
