:root{
    color-scheme: dark;

    --bg-app:        #0d0d0d;
    --bg-panel:      #141414;
    --bg-element:    #1f1f1f;
    --bg-active:     #2a2a2a;

    --border-dim:    #262626;
    --border-mid:    #333333;
    --border-bright: #555555;

    --text-main:     #ececec;
    --text-muted:    #a9a9a9;
    --text-accent:   #e6e6e6;

    --accent:        #3b82f6;
    --accent-dim:    rgba(59,130,246,0.15);

    --radius-sm:     3px;
    --radius-md:     6px;
    --spacing:       16px;

    --font-ui:   -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: "SF Mono","Menlo","Monaco","Consolas",monospace;
}

*{ box-sizing:border-box; }

/* FIX 1: Flex column on body handles "real server" header injections better */
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
    background: var(--bg-app);
    color: var(--text-main);
    font-family: var(--font-ui);
    font-size: 14px;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    display: flex;
    flex-direction: column;
}

a{ color:var(--accent); text-decoration:none; }
a:hover{ text-decoration:underline; }

.mono{ font-family:var(--font-mono); letter-spacing:-0.02em; }
.small{ font-size:0.85rem; color:var(--text-muted); }
.hideSm{ display:inline; }
@media (max-width:720px){ .hideSm{ display:none; } }

/* Scrollbars */
::-webkit-scrollbar{ width:8px; height:8px; }
::-webkit-scrollbar-track{ background:transparent; }
::-webkit-scrollbar-thumb{ background:var(--border-mid); border-radius:4px; }
::-webkit-scrollbar-thumb:hover{ background:var(--border-bright); }

/* =========================================================
   TOPBAR
   ========================================================= */
.topbar{
    display:grid;
    grid-template-columns:auto 1fr auto;
    gap:24px;
    align-items:center;

    /* FIX 2: Rigid height that won't shrink */
    height: 70px;
    flex: 0 0 70px;

    padding:0 var(--spacing);
    background:var(--bg-panel);
    border-bottom:1px solid var(--border-mid);
}

.brand{ display:flex; flex-direction:column; justify-content:center; }
.brand .title{
    font-weight:700;
    font-size:16px;
    letter-spacing:-0.01em;
    color:var(--text-main);
    display:flex;
    align-items:center;
    gap:10px;
}
.brand .subtitle{
    font-size:12px;
    color:var(--text-muted);
    margin-top:2px;
}

.tag, .brand .tag{ display:none !important; }
#themeToggle{ display:none !important; }

.togglePill{
    display:flex;
    align-items:center;
    gap:8px;
    font-size:12px;
    color:var(--text-muted);
    user-select:none;
    cursor:pointer;
}
.togglePill input{ margin:0; }

.controls{
    display:flex;
    align-items:flex-end;
    gap:12px;
    flex-wrap:wrap;
}

.field{
    display:flex;
    flex-direction:column;
    gap:4px;
}
.field label{
    font-size:10px;
    text-transform:uppercase;
    letter-spacing:0.05em;
    color:var(--text-muted);
    font-weight:600;
}

input[type="text"],
input[type="number"]{
    background:var(--bg-app);
    border:1px solid var(--border-dim);
    color:var(--text-main);
    border-radius:var(--radius-sm);
    padding:6px 8px;
    font-family:var(--font-mono);
    font-size:12px;
    transition:border-color 0.15s, background 0.15s;
    outline:none;
}

input[type="text"]:focus,
input[type="number"]:focus{
    border-color:var(--accent);
    background:var(--bg-element);
}

#seed{ min-width:180px; }
.controls input[type="number"]{ width:70px; }

.btn{
    background:var(--text-main);
    color:var(--bg-app);
    border:none;
    font-weight:600;
    padding:0 16px;
    height:32px;
    border-radius:var(--radius-sm);
    cursor:pointer;
    font-size:12px;
    text-transform:uppercase;
    letter-spacing:0.02em;
    transition:opacity 0.2s, transform 0.1s;
    margin-top:14px;
}
.btn:hover{ opacity:0.9; }
.btn:active{ transform:translateY(1px); }

#runBtn::after{ content:""; }

.toggle, #beginnerMode{ display:none !important; }

body.loading .topbar{
    box-shadow: inset 0 -2px 0 var(--accent);
}

/* =========================================================
   MAIN LAYOUT
   ========================================================= */

.layout{
    /* FIX 3: Flex 1 fills remaining space, but overflow:hidden prevents
       it from growing beyond the screen. */
    flex: 1;
    min-height: 0;
    overflow: hidden;

    display: grid;
    grid-template-columns: 260px 280px 1fr;

    background:var(--bg-app);
}

/* Panels */
.panel{
    display:flex;
    flex-direction:column;

    /* FIX 4: Ensure panels respect the parent's boundary */
    min-height: 0;
    overflow: hidden;

    border-right:1px solid var(--border-mid);
    background:var(--bg-app);
}
.panel:last-child{ border-right:none; }

@media (max-width: 1000px){
    /* Restore scrolling for mobile */
    html, body { overflow: auto; display: block; height: auto; }
    .layout{ display:block; height:auto; overflow:visible; }
    .panel{ border-right:none; border-bottom:1px solid var(--border-mid); overflow:visible; }
}

.panelHeader{
    padding:12px 16px;
    border-bottom:1px solid var(--border-dim);
    background:var(--bg-panel);
    display:flex;
    justify-content:space-between;
    align-items:center;
    flex-shrink:0;
    height: 48px;
}
.panelTitle{
    font-size:12px;
    font-weight:700;
    text-transform:uppercase;
    color:var(--text-main);
    letter-spacing:0.05em;
    display:flex;
    gap:16px;
}
.panelHint{
    font-size:11px;
    color:var(--text-muted);
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
    opacity:0.95;
}

#knobsPanel{ background:var(--bg-panel); }
.knobsScroll{
    padding:20px;
    overflow:auto;
    flex:1;
}

.knobs{ padding:0; }
.knobGrid{ display:grid; gap:16px; }

.knobGrid .field{
    display:grid;
    grid-template-columns:1fr 72px;
    align-items:center;
    gap:10px;
}
.knobGrid .field label{
    font-size:12px;
    color:var(--text-muted);
    text-transform:none;
    letter-spacing:0;
    font-weight:500;
    overflow-wrap:anywhere;
}
.knobGrid .field input{
    width:100%;
    text-align:right;
}

.knobs .panelTitle,
.knobs .panelHint{ display:none !important; }

#timelinePanel{ background:var(--bg-panel); }

.sliderRow{
    padding:16px;
    border-bottom:1px solid var(--border-dim);
    display:flex;
    align-items:center;
    gap:12px;
    flex-shrink:0;
}

#snapSlider{
    flex:1;
    cursor:pointer;
    accent-color: var(--accent);
}
#snapLabel{
    font-size:12px;
    color:var(--text-accent);
    width:52px;
    text-align:right;
}

.snapshotList{
    padding:12px;
    overflow-y:auto;
    min-height:0;
    flex:1;
    display:flex;
    flex-direction:column;
    gap:4px;
}

.snapItem,
.snapshotList button,
.snapshotList .item{
    padding:10px 12px;
    border-radius:var(--radius-sm);
    border:1px solid transparent;
    cursor:pointer;
    font-size:13px;
    color:var(--text-muted);
    transition:background 0.12s, color 0.12s, border-color 0.12s;
    background:transparent;
    text-align:left;
}

.snapItem:hover,
.snapshotList button:hover,
.snapshotList .item:hover{
    background:var(--bg-element);
    color:var(--text-main);
}

.snapItem.active,
.snapshotList button.active,
.snapshotList .active{
    background:var(--bg-active);
    border-color:var(--border-mid);
    color:var(--text-main);
    font-weight:500;
    box-shadow: 2px 0 0 var(--accent) inset;
}

.snapshotView{
    padding:24px;
    overflow:auto;
    flex:1;
    min-height:0;
}

.panel:nth-child(3) .panelHeader{
    position: sticky;
    top: 0;
    z-index: 20;
    background: var(--bg-panel);
}
.panel:nth-child(3) .snapshotView{
    padding-top: 16px;
}

.card{
    background:transparent;
    border:1px solid var(--border-mid);
    border-radius:var(--radius-md);
    padding:0;
    margin-bottom:24px;
    overflow:hidden;
}

.card h3{
    margin:0;
    padding:12px 16px;
    background:var(--bg-element);
    font-size:12px;
    text-transform:uppercase;
    letter-spacing:0.05em;
    color:var(--text-muted);
    border-bottom:1px solid var(--border-dim);
}

.grid2{
    display:grid;
    grid-template-columns:1fr 1fr;
    border-bottom:1px solid var(--border-dim);
}
@media (max-width: 720px){
    .grid2{ grid-template-columns:1fr; }
}
.kv{
    display:flex;
    justify-content:space-between;
    padding:12px 16px;
    border-right:1px solid var(--border-dim);
    gap:10px;
}
.kv:nth-child(even){ border-right:none; }
@media (max-width: 720px){
    .kv{ border-right:none; border-bottom:1px solid var(--border-dim); }
    .kv:last-child{ border-bottom:none; }
}
.kv .k{ color:var(--text-muted); font-size:12px; }
.kv .v{ color:var(--text-accent); font-family:var(--font-mono); font-size:13px; }

.cmpBlock, .sentence{
    border:1px solid var(--border-mid);
    border-radius:var(--radius-md);
    margin-top:16px;
    overflow:hidden;
}

.cmpHead{
    background:var(--bg-element);
    padding:10px 12px;
    font-size:11px;
    text-transform:uppercase;
    font-weight:700;
    color:var(--text-muted);
    border-bottom:1px solid var(--border-dim);
}

.cmpGrid{
    display:grid;
    grid-template-columns:140px 1fr;
    gap:18px;
    align-items:start;
    padding:14px 12px 16px 12px;
}

.cmpVar{
    font-size:11px;
    color:var(--text-muted);
    text-align:right;
    margin-top:6px;
    white-space:nowrap;
}

.cmpLines{
    background:var(--bg-app);
    border:1px solid var(--border-dim);
    border-radius:var(--radius-sm);
    padding:10px 10px;
}

.cmpLines .line{
    display:flex;
    gap:12px;
    padding:6px 0;
    font-family:var(--font-mono);
    font-size:13px;
    line-height:1.45;
    border-bottom:1px dashed var(--border-dim);
}
.cmpLines .line:last-child{ border-bottom:none; }

.cmpEnglish{
    padding:12px;
    color:var(--text-muted);
    border-bottom:1px solid var(--border-dim);
}
.cmpEnglish .lbl{ color:var(--text-muted); font-weight:700; }
.cmpEnglish .txt{ color:var(--text-main); }

.sentence{ padding:0; }
.sentence .line{
    display:grid;
    grid-template-columns:80px 1fr;
    gap:12px;
    padding:10px 12px;
    border-bottom:1px dashed var(--border-dim);
}
.sentence .line:last-child{ border-bottom:none; }
.sentence .lbl{
    font-size:11px;
    text-transform:uppercase;
    font-weight:700;
    color:var(--text-muted);
}
.sentence .txt{
    font-family:var(--font-mono);
    font-size:13px;
    line-height:1.5;
    color:var(--text-main);
    overflow-wrap:anywhere;
}

.diffTok{ color:var(--text-main); }
.chgbox{
    background:rgba(255,255,255,0.10);
    color:#fff;
    padding:0 4px;
    border-radius:2px;
}

.footer{ display:none !important; }

pre{
    margin:0;
    padding:12px 16px;
    overflow:auto;
    background:var(--bg-app);
    border-top:1px solid var(--border-dim);
    font-family:var(--font-mono);
    font-size:12px;
    color:var(--text-main);
}

details{
    border-top:1px solid var(--border-dim);
    padding:12px 16px;
}
summary{ cursor:pointer; color:var(--text-muted); font-weight:600; }

.layout > .panel:nth-child(3){
    min-height: 0;
    overflow: hidden;
}

.layout > .panel:nth-child(3) .snapshotView{
    overflow: auto;
    min-height: 0;
    flex: 1;
    padding: 24px;
}

.layout > .panel:nth-child(3) .panelHeader{
    position: sticky;
    top: 0;
    z-index: 50;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border-dim);
}

.layout > .panel:nth-child(3) .snapshotView::-webkit-scrollbar{ width: 10px; }
.layout > .panel:nth-child(3) .snapshotView::-webkit-scrollbar-thumb{
    background: var(--border-mid);
    border-radius: 6px;
}
.layout > .panel:nth-child(3) .snapshotView::-webkit-scrollbar-thumb:hover{
    background: var(--border-bright);
}

.qmark{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    width: 16px;
    height: 16px;
    border-radius: 999px;
    font-size: 10px;
    font-weight: 700;
    color: var(--text-muted);
    border: 1px solid var(--border-dim);
    background: var(--bg-app);
    cursor: help;
    position: relative;
}

.qmark:hover::after{
    content: attr(data-tip);
    position: absolute;
    z-index: 9999;
    top: 22px;
    left: 0;

    min-width: 220px;
    max-width: 360px;

    padding: 10px 10px;
    border-radius: var(--radius-md);

    background: var(--bg-panel);
    border: 1px solid var(--border-mid);
    color: var(--text-main);

    font-size: 12px;
    line-height: 1.35;
    white-space: normal;

    box-shadow: 0 16px 40px rgba(0,0,0,0.6);
    pointer-events: none;
}

.panelTabs{
    display: flex;
    gap: 0;
    height: 100%;
    align-items: center;
}

.tab{
    background: transparent;
    border: none;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-muted);
    padding: 0 12px;
    height: 100%;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: color 0.15s, border-color 0.15s;
}

.tab:hover{
    color: var(--text-main);
}

.tab.active{
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.dictControls{
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
    align-items: center;
}

.dictSearch{
    flex: 1;
    max-width: 300px;
}

.dictList{
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.dictItem{
    display: grid;
    grid-template-columns: 140px 1fr 100px 80px;
    align-items: center;
    padding: 12px;
    border: 1px solid var(--border-mid);
    border-radius: var(--radius-sm);
    background: var(--bg-app);
    gap: 16px;
}

.dictWord{
    font-family: var(--font-mono);
    font-size: 14px;
    font-weight: 700;
    color: var(--accent);
}

.dictMeaning{
    color: var(--text-main);
    font-size: 13px;
}

.dictPos{
    font-size: 11px;
    text-transform: uppercase;
    color: var(--text-muted);
    font-weight: 600;
    letter-spacing: 0.05em;
    text-align: right;
}

.dictEtym{
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 4px;
    text-align: center;
    font-weight: 700;
    text-transform: uppercase;
}

.etym-L0{ background: rgba(255,255,255,0.08); color: var(--text-muted); }
.etym-S0{ background: rgba(59,130,246,0.15); color: var(--accent); }

.dictStats{
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--border-dim);
    color: var(--text-muted);
    font-size: 12px;
}