/* ── Reset ── */
*{box-sizing:border-box;margin:0;padding:0}

/* ── Base ── */
body{
  font-family:'Segoe UI',system-ui,sans-serif;
  background:#f5f7fa;
  color:#1a202c;
  font-size:15px;
  transition:font-size 0.2s;
  line-height:1.6;
}
h1{color:#1a202c;font-size:1.3em;font-weight:700}
h2{color:#1e3a5f;font-size:1.05em;font-weight:600;margin-bottom:6px}
h3{color:#2d5282;font-size:0.95em;font-weight:600;margin-bottom:5px}
p{color:#2d3748;margin-bottom:10px;line-height:1.7}
code{background:#e8f0fe;color:#1a56db;padding:1px 6px;border-radius:4px;font-size:0.88em;font-family:monospace}
ul,ol{color:#2d3748;padding-left:20px}
li{margin-bottom:4px}
strong{color:#1a202c}

/* ── Layout ── */
#app{display:flex;flex-direction:column;min-height:100vh}

#topbar{
  background:#1e3a5f;
  color:#fff;
  padding:10px 20px;
  display:flex;
  align-items:center;
  justify-content:space-between;
  position:sticky;top:0;z-index:100;
  border-bottom:2px solid #2d5282;
}
#topbar h1{color:#fff;font-size:1.05em}

.zoom-controls{display:flex;gap:6px;align-items:center}
.zoom-controls button{
  background:#2d5282;color:#fff;border:none;
  padding:5px 12px;border-radius:5px;cursor:pointer;font-size:0.9em;
}
.zoom-controls button:hover{background:#3b6cb7}
.zoom-controls span{color:#93c5fd;font-size:0.85em}

#main-layout{
  display:flex;flex:1;
  height:calc(100vh - 50px);
  overflow:hidden;
  transition:all 0.3s;
}

/* ── Sidebar ── */
#sidebar{
  width:55%;
  overflow-y:auto;overflow-x:hidden;
  padding:18px 28px 18px 18px;
  background:#fff;
  border-right:2px solid #cbd5e0;
  transition:width 0.3s,padding 0.3s;
  position:relative;
  height:100%;
}

/* ── Main Panel ── */
#mainpanel{
  width:45%;
  display:flex;flex-direction:column;
  background:#eef2f7;
  border-left:2px solid #cbd5e0;
  transition:width 0.3s;
  position:relative;height:100%;
}

#mp-titlebar{
  background:#1e3a5f;
  color:#fff;
  padding:10px 16px;
  display:flex;align-items:center;justify-content:space-between;
  flex-shrink:0;
}
#mp-titlebar h2{color:#fff;font-size:0.95em;margin:0}

#mp-content{
  flex:1;overflow-y:auto;overflow-x:hidden;
  padding:14px;
  position:relative;
}

/* ── Collapse ── */
#sidebar.collapsed{width:0;padding:0;overflow:hidden;border:none}
#mainpanel.collapsed{width:0;overflow:hidden;border:none}
#sidebar.expanded{width:100%}
#mainpanel.expanded{width:100%}

.panel-toggle{
  position:absolute;top:50%;transform:translateY(-50%);
  z-index:50;background:#1e3a5f;color:#fff;border:none;
  width:18px;height:56px;cursor:pointer;
  display:flex;align-items:center;justify-content:center;
  font-size:0.8em;transition:background 0.2s;
}
.panel-toggle:hover{background:#2d5282}
#sidebar-toggle{right:0;border-radius:4px 0 0 4px;z-index:40}
#mainpanel-toggle{left:0;border-radius:0 4px 4px 0}

/* ── Sidebar Sections ── */
.section{
  background:#fff;
  border:1px solid #e2e8f0;
  border-left:4px solid #3b82f6;
  border-radius:8px;
  overflow:hidden;
  margin-bottom:14px;
  box-shadow:0 1px 3px rgba(0,0,0,0.06);
}
.section.green{border-left-color:#16a34a}
.section.purple{border-left-color:#7c3aed}
.section.yellow{border-left-color:#ca8a04}
.section.red{border-left-color:#dc2626}
.section.cyan{border-left-color:#0891b2}
.section.yellow{border-left-color:#ca8a04}

.section-header{
  display:flex;align-items:center;justify-content:space-between;
  padding:12px 16px;cursor:pointer;user-select:none;
  background:#f8fafc;
}
.section-header:hover{background:#f1f5f9}
.section-header h2{margin-bottom:0;flex:1;color:#1e3a5f}
.section-toggle{color:#94a3b8;font-size:0.9em;transition:transform 0.2s}
.section.collapsed .section-body{display:none}
.section.collapsed .section-toggle{transform:rotate(-90deg)}
.section-body{padding:0 16px 16px}

/* ── Tables ── */
table{width:100%;border-collapse:collapse;margin-top:8px;font-size:0.85em}
th{background:#dbeafe;color:#1e3a5f;padding:7px 10px;text-align:left;font-weight:600}
td{padding:6px 10px;border-bottom:1px solid #e2e8f0;color:#374151}
td:first-child{color:#1a56db;font-weight:600;white-space:nowrap}
tr:hover td{background:#f8fafc}

/* ── Flow Diagram ── */
#flow{display:flex;flex-direction:column;align-items:center}
.flow-step{
  background:#dbeafe;color:#1e3a5f;
  border:1px solid #93c5fd;
  border-radius:7px;padding:9px 16px;
  text-align:center;font-size:0.85em;
  font-weight:500;width:290px;
}
.flow-step.highlight{background:#ede9fe;color:#4c1d95;border-color:#c4b5fd}
.flow-step.green{background:#dcfce7;color:#14532d;border-color:#86efac}
.flow-step.orange{background:#ffedd5;color:#7c2d12;border-color:#fdba74}
.flow-arrow{width:2px;height:18px;background:#94a3b8;margin:0 auto}

/* ── Buttons ── */
.btn{
  background:#1e3a5f;color:#fff;border:none;
  padding:7px 16px;border-radius:6px;cursor:pointer;
  font-size:0.88em;margin-top:10px;font-weight:500;
  transition:background 0.15s;
}
.btn:hover{background:#2d5282}
.btn:disabled{background:#94a3b8;cursor:not-allowed;color:#e2e8f0}

/* ── Output Boxes ── */
.out-box{
  background:#f0f4f8;
  border:1px solid #cbd5e0;
  border-radius:6px;padding:8px 10px;
  margin-top:6px;
  font-family:monospace;font-size:0.76em;
  color:#1a202c;
  max-height:180px;overflow-y:auto;word-break:break-all;
}
.out-box.matrix{max-height:280px}

/* ── Info Cards ── */
.info-row{display:flex;gap:8px;flex-wrap:wrap;margin-top:8px}
.info-card{
  background:#f0f4f8;border:1px solid #e2e8f0;
  border-radius:6px;padding:8px 12px;flex:1;min-width:110px;
}
.info-card .val{color:#1e3a5f;font-size:1.05em;font-weight:700}
.info-card .lbl{color:#64748b;font-size:0.73em;margin-top:2px}

/* ── Formula ── */
.formula{
  background:#f0f4f8;
  border-left:3px solid #7c3aed;
  padding:9px 12px;border-radius:4px;
  font-family:monospace;color:#1a202c;
  margin:8px 0;font-size:0.88em;
}

/* ── Key Display ── */
.key-display{
  background:#f0f4f8;border:1px solid #16a34a;
  border-radius:6px;padding:9px;margin-top:8px;
  font-family:monospace;font-size:0.72em;
  color:#1a202c;word-break:break-all;
  max-height:110px;overflow-y:auto;
}

/* ── Sign num badge ── */
.sign-num{
  background:#1e3a5f;color:#fff;border-radius:50%;
  width:22px;height:22px;display:flex;align-items:center;
  justify-content:center;font-size:0.72em;flex-shrink:0;margin-top:2px;
}
.sign-text{font-size:0.85em;color:#2d3748;line-height:1.6}
.sign-text strong{color:#1e3a5f}

/* ── Main Panel Sections ── */
.mp-section{
  background:#fff;border:1px solid #e2e8f0;
  border-left:3px solid #3b82f6;
  border-radius:7px;overflow:hidden;
  margin-bottom:12px;
  box-shadow:0 1px 2px rgba(0,0,0,0.05);
}
.mp-section.red{border-left-color:#dc2626}
.mp-section.green{border-left-color:#16a34a}

.mp-section-header{
  display:flex;align-items:center;justify-content:space-between;
  padding:9px 13px;cursor:pointer;user-select:none;
  background:#f8fafc;
}
.mp-section-header:hover{background:#f1f5f9}
.mp-section-header h3{color:#1e3a5f;font-size:0.88em;margin:0;flex:1}
.mp-section-toggle{color:#94a3b8;font-size:0.85em;transition:transform 0.2s}
.mp-section.collapsed .mp-section-body{display:none}
.mp-section.collapsed .mp-section-toggle{transform:rotate(-90deg)}
.mp-section-body{padding:0 13px 13px}

.mp-label{color:#475569;font-size:0.75em;font-weight:600;margin-top:10px;margin-bottom:3px;text-transform:uppercase;letter-spacing:0.03em}
.mp-size{color:#1e3a5f;font-size:0.75em;font-weight:600;margin-top:5px;text-align:right}

/* ── Polynomial Column Vectors ── */
.poly-matrix-wrap{
  overflow-x:auto;overflow-y:hidden;
  margin-top:6px;padding-bottom:4px;
  border:1px solid #cbd5e0;border-radius:6px;
  background:#f8fafc;
}
.poly-matrix{display:flex;flex-direction:row;min-width:max-content}
.poly-col{display:flex;flex-direction:column;min-width:100px;border-right:1px solid #e2e8f0}
.poly-col:last-child{border-right:none}
.poly-col-header{
  background:#dbeafe;color:#1e3a5f;
  font-size:0.74em;font-weight:700;
  padding:5px 8px;text-align:center;
  border-bottom:1px solid #bfdbfe;
  white-space:nowrap;position:sticky;top:0;z-index:5;
}
.poly-col-body{height:290px;overflow-y:auto;display:flex;flex-direction:column}
.poly-coeff{
  font-family:monospace;font-size:0.7em;
  padding:2px 8px;
  border-bottom:1px solid #f1f5f9;
  text-align:right;white-space:nowrap;
  color:#1a202c;
}
.poly-coeff:nth-child(odd){background:#f8fafc}
.poly-coeff:nth-child(even){background:#fff}
.poly-coeff .ci{color:#94a3b8;font-size:0.82em;margin-right:6px;user-select:none}
.poly-col-footer{
  background:#dbeafe;color:#1e3a5f;
  font-size:0.68em;font-weight:600;
  padding:4px 8px;text-align:center;
  border-top:1px solid #bfdbfe;
  white-space:nowrap;position:sticky;bottom:0;
}

/* ── Reference badges ── */
.ref{
  display:inline-block;
  font-size:0.68em;font-weight:600;
  background:#dbeafe;color:#1e3a5f;
  border:1px solid #93c5fd;
  border-radius:4px;padding:1px 6px;
  margin-left:5px;vertical-align:middle;
  text-decoration:none;cursor:pointer;
  white-space:nowrap;
  transition:background 0.15s;
}
.ref:hover{background:#bfdbfe;color:#1e40af}
.ref-green{background:#dcfce7;color:#14532d;border-color:#86efac}
.ref-green:hover{background:#bbf7d0}
.ref-purple{background:#ede9fe;color:#4c1d95;border-color:#c4b5fd}
.ref-purple:hover{background:#ddd6fe}
.spinner{
  display:inline-block;width:13px;height:13px;
  border:2px solid #cbd5e0;border-top-color:#1e3a5f;
  border-radius:50%;animation:spin 0.7s linear infinite;
  vertical-align:middle;margin-right:5px;
}
@keyframes spin{to{transform:rotate(360deg)}}

/* ── API Status ── */
#api-status{
  font-size:0.72em;padding:3px 10px;
  border-radius:10px;margin-left:10px;font-weight:600;
}
#api-status.ok{background:#dcfce7;color:#14532d}
#api-status.err{background:#fee2e2;color:#991b1b}
#api-status.loading{background:#dbeafe;color:#1e3a5f}

/* ── Textarea ── */
textarea{
  background:#f8fafc;border:1px solid #cbd5e0;
  color:#1a202c;border-radius:6px;font-size:0.88em;
}
textarea:focus{outline:2px solid #3b82f6;border-color:#3b82f6}

/* ── Scrollbars (light) ── */
::-webkit-scrollbar{width:6px;height:6px}
::-webkit-scrollbar-track{background:#f1f5f9}
::-webkit-scrollbar-thumb{background:#cbd5e0;border-radius:3px}
::-webkit-scrollbar-thumb:hover{background:#94a3b8}

/* ── Slide-in animation for main panel sections ── */
@keyframes slideInDown{
  from{opacity:0;transform:translateY(-14px)}
  to{opacity:1;transform:translateY(0)}
}
.mp-slide-in{
  animation:slideInDown 0.35s ease forwards;
}

/* ── Highlighted new result ── */
@keyframes flashHighlight{
  0%{box-shadow:0 0 0 3px #3b82f6}
  100%{box-shadow:0 0 0 0 transparent}
}
.mp-slide-in{
  animation:slideInDown 0.35s ease forwards, flashHighlight 1s ease 0.35s forwards;
}

/* ── Responsive ── */
@media(max-width:768px){
  #main-layout{flex-direction:column;height:auto}
  #sidebar{width:100%;border-right:none;border-bottom:2px solid #cbd5e0;height:auto}
  #mainpanel{width:100%;height:50vh;border-left:none;border-top:2px solid #cbd5e0}
}
