/* ================================
   THEME VARIABLES
================================ */

:root {
    color-scheme: light;

    --bg-base:         #f5f6f8;
    --bg-surface:      #ffffff;
    --bg-surface-alt:  #fafafa;
    --bg-hover:        #f1f3f6;
    --bg-tab:          #eef0f4;
    --bg-controls:     #eef0f4;
    --bg-ref:          #f8f9fb;

    --text-primary:    #222222;
    --text-secondary:  #666666;
    --text-muted:      #888888;
    --text-heading:    #1e293b;

    --border:          #d0d4da;
    --border-light:    #e2e8f0;
    --border-input:    #c6cbd2;

    --navbar-bg:       #34163b;

    --color-primary:        #71397f;
    --chip-bg:              #f6f2f7;
    --chip-border:          #d5bddb;
    --chip-text:            #71397f;
    --chip-hover-bg:        #eadeed;
    --chip-hover-border:    #bd93c8;

    --bead-selected-bg:     #eee5f0;
    --bead-selected-border: #d5bddb;
    --bead-selected-accent: #9351a4;

    --toggle-off: #c6cbd2;
    --toggle-on:  #71397f;

    --color-success: #2bb673;
    --sasa-good:     #2bb673;
    --sasa-warn:     #d97706;
    --sasa-bad:      #dc2626;
}

[data-theme="dark"] {
    color-scheme: dark;

    --bg-base:         #0d1117;
    --bg-surface:      #161b22;
    --bg-surface-alt:  #1c2128;
    --bg-hover:        #21262d;
    --bg-tab:          #21262d;
    --bg-controls:     #161b22;
    --bg-ref:          #161b22;

    --text-primary:    #c9d1d9;
    --text-secondary:  #8b949e;
    --text-muted:      #6e7681;
    --text-heading:    #e6edf3;

    --border:          #30363d;
    --border-light:    #21262d;
    --border-input:    #30363d;

    --navbar-bg:       #201022;

    --color-primary:        #bd93c8;
    --chip-bg:              #2a192e;
    --chip-border:          #894e98;
    --chip-text:            #bd93c8;
    --chip-hover-bg:        #3c2442;
    --chip-hover-border:    #a465b3;

    --bead-selected-bg:     #2a192e;
    --bead-selected-border: #894e98;
    --bead-selected-accent: #bd93c8;

    --toggle-off: #30363d;
    --toggle-on:  #8f4ba0;

    --color-success: #3fb950;
    --sasa-good:     #3fb950;
    --sasa-warn:     #d29922;
    --sasa-bad:      #f85149;
}

/* ================================
   GLOBAL TYPOGRAPHY
================================ */

body {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    margin: 0;
    padding: 0;

    font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont,
                 "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-weight: 400;
    color: var(--text-primary);
    background: var(--bg-base);
}

/* ================================
   NAVBAR
================================ */

#navbar {
    display: flex;
    align-items: center;
    padding: 0 16px;
    background: var(--navbar-bg);
    color: white;
    height: 46px;
    flex-shrink: 0;
    gap: 8px;
}

.navbar-brand {
    display: flex;
    align-items: center;
    padding-right: 8px;
}

.navbar-logo {
    height: 32px;
    width: auto;
    display: block;
}

.navbar-tabs {
    display: flex;
    gap: 2px;
    height: 100%;
    align-items: stretch;
}

.navbar-tab {
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    border-radius: 0;
    color: rgba(255,255,255,0.6);
    font-size: 13px;
    font-weight: 500;
    padding: 0 14px;
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s, background 0.15s;
    white-space: nowrap;
    font-family: inherit;
}

.navbar-tab:hover {
    color: white;
    background: rgba(255,255,255,0.08);
}

.navbar-tab.active {
    color: white;
    border-bottom-color: #ac6bbd;
    font-weight: 600;
}

.navbar-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 4px;
}

.navbar-icon-link {
    display: flex;
    align-items: center;
    color: rgba(255,255,255,0.65);
    text-decoration: none;
    padding: 5px 7px;
    border-radius: 6px;
    border: none;
    background: none;
    cursor: pointer;
    font-family: inherit;
    transition: color 0.15s, background 0.15s;
}

.navbar-icon-link:hover {
    color: white;
    background: rgba(255,255,255,0.1);
}

/* Moon shown in light mode, sun in dark mode */
.theme-toggle .icon-sun  { display: none; }
.theme-toggle .icon-moon { display: block; }

[data-theme="dark"] .theme-toggle .icon-sun  { display: block; }
[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

/* ================================
   PAGE LAYOUT
================================ */

.page {
    display: none;
}

.page.active {
    display: block;
}

#page-app {
    display: none;
}

#page-app.active {
    display: grid;
    grid-template-columns: 60vw auto;
    grid-template-rows: auto 70vh auto;
    column-gap: 16px;
    row-gap: 14px;
    align-items: start;
}

/* ================================
   DOC PAGES
================================ */

.doc-page {
    padding: 48px 24px 64px;
    box-sizing: border-box;
}

.doc-content {
    max-width: 760px;
    margin: 0 auto;
    line-height: 1.65;
}

.doc-content img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 16px auto;
    border-radius: 6px;
}

.doc-content h1 .doc-heading-logo {
    display: block;
    height: 56px;
    width: auto;
    margin: 0;
    border-radius: 0;
    position: relative;
    top: -12px;
}

.doc-content details {
    border: 1px solid var(--border);
    border-radius: 6px;
    margin-bottom: 12px;
    overflow: hidden;
}

.doc-content summary {
    padding: 10px 14px;
    font-weight: 600;
    cursor: pointer;
    background: var(--bg-controls);
    list-style: none;
    user-select: none;
}

.doc-content summary::before {
    content: '▶ ';
    font-size: 10px;
    color: var(--text-muted);
}

.doc-content details[open] summary::before {
    content: '▼ ';
}

.doc-content details > *:not(summary) {
    padding: 12px 16px;
}

.doc-content details.nested {
    border: none;
    border-left: 3px solid var(--border-input);
    border-radius: 0;
    margin: 0;
    padding-left: 14px;
}

.doc-content details.nested + details.nested {
    border-top: 1px solid var(--border-light);
    padding-top: 2px;
}

.doc-content details.nested > summary {
    font-size: 15px;
    font-weight: 600;
    padding: 10px 0;
    background: none;
}

.doc-content details.nested > *:not(summary) {
    padding: 0 0 14px;
}

.doc-content h1 {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 24px;
    font-weight: 700;
    margin: 0 0 24px;
    color: var(--text-heading);
    letter-spacing: -0.02em;
}

.doc-content h2 {
    display: flex;
    align-items: center;
    gap: 9px;
    font-size: 16px;
    font-weight: 600;
    margin: 32px 0 10px;
    color: var(--text-heading);
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
}

.doc-content h2 svg {
    flex-shrink: 0;
    color: var(--color-primary);
}

.doc-content .toc {
    margin: 0 0 28px;
    padding: 14px 18px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-surface);
    font-size: 13px;
    line-height: 1.5;
}

.doc-content .toc-title {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.doc-content .toc ol {
    margin: 0;
    padding-left: 18px;
    columns: 2;
}

/* ================================
   CALLOUT BOXES (DOC PAGES)
================================ */

.callout {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 12px 14px;
    border-radius: 8px;
    border: 1px solid var(--border);
    margin: 14px 0;
    font-size: 0.95em;
    line-height: 1.55;
}

.callout svg {
    flex-shrink: 0;
    margin-top: 3px;
}

.callout p {
    margin: 0;
}

.callout-warning {
    background: color-mix(in srgb, var(--sasa-warn) 8%, var(--bg-surface));
    border-color: var(--sasa-warn);
}

.callout-warning svg {
    color: var(--sasa-warn);
}

.callout-note {
    background: var(--chip-bg);
    border-color: var(--chip-border);
}

.callout-note svg {
    color: var(--chip-text);
}

/* ================================
   BEAD TYPE COLOR SWATCHES (DOC PAGES)
================================ */

.color-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 5px;
    vertical-align: middle;
}

/* Matches typeColor() in visualization.js exactly. */
.color-dot-c { background: #8c8c8c; } /* apolar */
.color-dot-n { background: #4a8fd9; } /* intermediate */
.color-dot-p { background: #e84d3d; } /* polar */
.color-dot-q { background: #f29c12; } /* charged */
.color-dot-x { background: #2ecc70; } /* halogen */

/* Matches the Mapping panel's Mismatch indicator (sasa-diff-good/-warn/-bad). */
.color-dot-good { background: var(--sasa-good); }
.color-dot-warn { background: var(--sasa-warn); }
.color-dot-bad  { background: var(--sasa-bad); }

.doc-content ol,
.doc-content ul:not(.ref-list) {
    margin: 0 0 16px 20px;
    padding: 0;
}

.doc-content li {
    margin: 8px 0;
}

.doc-content a,
.info-footnote a {
    color: var(--color-primary);
    text-decoration: none;
}

.doc-content a:hover,
.info-footnote a:hover {
    text-decoration: underline;
}

/* ================================
   TOP CONTROLS (APP PAGE)
================================ */

#controls {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    grid-column: 1 / 3;
    padding: 5px 10px;
}

#load-panel {
    display: flex;
    flex-direction: column;
    padding: 6px 10px;
    background: var(--bg-controls);
    border: 1px solid var(--border-input);
    border-radius: 6px;
    font-size: 12px;
    align-self: stretch;
    gap: 4px;
}

.load-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

#mol-select { width: 210px; font-size: 12px; }

.load-secondary {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

#load-panel button {
    font-size: 12px;
    padding: 3px 10px;
    white-space: nowrap;
}

/* ================================
   VIEWER
================================ */

#structview {
    grid-column: 1;
    grid-row: 2;
    height: 70vh;
    width: 100%;
    background: black;
}

#viewport {
    height: 100%;
    width: 100%;
}

/* ================================
   BEAD LIST (RIGHT PANEL)
================================ */

#beadlist {
    grid-column: 2;
    grid-row: 2;

    height: 70vh;
    box-sizing: border-box;

    display: flex;
    flex-direction: column;
    background: var(--bg-surface);
    border-radius: 8px;

    min-height: 0;
}

#bead-actions {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#bead-actions button {
    font-size: 12px;
    padding: 4px 8px;
}

#bead-list {
    list-style: none;
    padding: 8px 12px;
    margin: 0;

    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
}

#bead-list ul {
    margin: 0.3em 0 0 1.2em;
    padding-left: 1.2em;
}

/* Individual bead cards */
.bead-view {
    margin: 0.6em 0;
    padding: 0.7em;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-surface-alt);
    box-sizing: border-box;
    transition: background 0.2s ease;
    font-size: 14px;
}

.field-label {
    font-size: 11px;
    color: var(--text-secondary);
    margin-bottom: 2px;
}

.bead-view:hover {
    background: var(--bg-hover);
}

.selected-bead {
    background: var(--bead-selected-bg);
    border-color: var(--bead-selected-border);
    border-left: 3px solid var(--bead-selected-accent);
    padding-left: calc(0.7em - 2px);
}

.selected-bead input {
    background: var(--bg-surface);
    color: var(--text-primary);
    border-color: var(--border-input);
}

.selected-bead .field-label {
    color: var(--text-secondary);
}

.bead-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.bead-fields {
    display: grid !important;
    grid-template-columns: 2fr 1fr 1fr;
    gap: 10px;
    flex: 1;
}

.bead-fields .field {
    min-width: 0;
}

input.input-error {
    border-color: var(--sasa-bad) !important;
    color: var(--sasa-bad);
}

.field:has(input.input-error) .field-label {
    color: var(--sasa-bad);
}

.field:has(input.input-error) .field-label::after {
    content: ' ⚠';
}

.bead-fields input {
    width: 100%;
    box-sizing: border-box;
}

/* ================================
   FILE OUTPUTS + TABS
================================ */

#file-outputs {
    grid-column: 1 / 3;
    grid-row: 3;
    padding: 0 16px 14px;
    box-sizing: border-box;
}

.tab-bar {
    display: flex;
    gap: 2px;
    border-bottom: 2px solid var(--border);
    margin-bottom: 10px;
    align-items: flex-end;
}

.tab-btn {
    padding: 5px 16px;
    border: 1px solid transparent;
    border-bottom: none;
    border-radius: 4px 4px 0 0;
    background: var(--bg-tab);
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    position: relative;
    bottom: -2px;
    transition: background 0.15s, color 0.15s;
}

.tab-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.tab-btn.active {
    background: var(--bg-surface);
    border-color: var(--border);
    color: var(--text-primary);
    font-weight: 600;
}

.tab-panel {
    display: none;
}

.tab-panel.active {
    display: block;
}

#file-outputs pre {
    border: 1px solid var(--border);
    padding: 1em;
    margin: 0.6em 0 0;
    background: var(--bg-surface);
    color: var(--text-primary);
    border-radius: 6px;
    overflow-x: auto;
    max-height: 180px;
    overflow-y: auto;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 13px;
}

/* ================================
   CONTROLS SEPARATORS
================================ */

.controls-sep {
    width: 1px;
    height: 26px;
    background: var(--border);
    align-self: center;
    flex-shrink: 0;
}

/* ================================
   COLLAPSIBLE ATOM LIST
================================ */

.atom-list-details {
    margin-top: 4px;
}

.atom-list-summary {
    cursor: pointer;
    font-size: 11px;
    color: var(--text-muted);
    list-style: none;
    padding: 1px 0;
    user-select: none;
}

.atom-list-summary::-webkit-details-marker { display: none; }

.atom-list-summary::before {
    content: '▸ ';
    font-size: 10px;
}

details[open] .atom-list-summary::before {
    content: '▾ ';
}

.output-header {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 6px;
}

.output-warning {
    padding: 6px 10px;
    margin-bottom: 6px;
    background: color-mix(in srgb, var(--sasa-warn) 10%, var(--bg-surface));
    border: 1px solid var(--sasa-warn);
    border-radius: 6px;
    font-size: 12px;
    color: var(--sasa-warn);
    font-weight: 500;
}

/* A genuine error (output will definitely be wrong/faulty), as opposed to
   the orange default above (an advisory condition worth checking). */
.output-warning.output-error {
    background: color-mix(in srgb, var(--sasa-bad) 10%, var(--bg-surface));
    border-color: var(--sasa-bad);
    color: var(--sasa-bad);
}

#beadlist > .output-warning {
    margin: 8px 12px 0;
}

.hint-text {
    margin: 8px 12px 0;
    font-size: 12px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 5px;
}

.hint-text svg {
    flex-shrink: 0;
}

/* ================================
   INFO FOOTNOTE
================================ */

.info-footnote {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 6px;
}

/* ================================
   BUTTONS + INPUTS
================================ */

button {
    font-family: inherit;
    font-weight: 500;
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid var(--border-input);
    background: var(--bg-surface);
    color: var(--text-primary);
    cursor: pointer;
    transition: background 0.2s ease, box-shadow 0.2s ease;
}

button:hover {
    background: var(--bg-hover);
}

button:disabled {
    opacity: 0.5;
    cursor: default;
}

input {
    font-family: inherit;
    padding: 4px 6px;
    border-radius: 4px;
    border: 1px solid var(--border-input);
    background: var(--bg-surface);
    color: var(--text-primary);
}

/* Scrollbar */
#bead-list::-webkit-scrollbar {
    width: 8px;
}

#bead-list::-webkit-scrollbar-thumb {
    background: var(--border-input);
    border-radius: 4px;
}

.bead-btn-col {
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-self: flex-start;
    flex-shrink: 0;
}

.delete-bead,
.predict-bead {
    font-size: 11px;
    padding: 4px 8px;
    white-space: nowrap;
    width: 100%;
}

button.copied {
    background-color: var(--color-success);
    color: white;
    border-color: var(--color-success);
}

/* ================================
   TOGGLE SWITCHES
================================ */

.toggle-switch {
    display: flex;
    align-items: center;
    gap: 6px;
    cursor: pointer;
    user-select: none;
    font-weight: 500;
}

.toggle-switch input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-track {
    position: relative;
    width: 32px;
    height: 18px;
    background: var(--toggle-off);
    border-radius: 9px;
    transition: background 0.2s ease;
    flex-shrink: 0;
}

.toggle-switch input:checked + .toggle-track {
    background: var(--toggle-on);
}

.toggle-thumb {
    position: absolute;
    top: 2px;
    left: 2px;
    width: 14px;
    height: 14px;
    background: white;
    border-radius: 50%;
    transition: transform 0.2s ease;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle-switch input:checked + .toggle-track .toggle-thumb {
    transform: translateX(14px);
}

.toggle-switch input:disabled + .toggle-track {
    opacity: 0.5;
}

.toggle-switch:has(input:disabled) {
    cursor: default;
    opacity: 0.6;
}

/* ================================
   BEAD TYPE CHIP
================================ */

.type-field-wrap {
    min-height: 18px;
}

.bead-type-chip {
    margin-top: 3px;
    font-size: 11px;
    padding: 2px 7px;
    background: var(--chip-bg);
    border: 1px solid var(--chip-border);
    border-radius: 4px;
    color: var(--chip-text);
    cursor: pointer;
    font-weight: 600;
    white-space: nowrap;
}

.bead-type-chip:hover {
    background: var(--chip-hover-bg);
    border-color: var(--chip-hover-border);
}

/* ================================
   VIEWER PANEL
================================ */

#viewer-panel button,
#viewer-panel .toggle-switch {
    font-size: 12px;
}

#viewer-panel {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 6px 10px;
    background: var(--bg-controls);
    border: 1px solid var(--border-input);
    border-radius: 6px;
    font-size: 12px;
    align-self: stretch;
}

.viewer-row {
    display: flex;
    gap: 12px;
    align-items: center;
}

/* ================================
   SASA PANEL
================================ */

#sasa-panel, #mapping-panel {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 6px 10px;
    align-self: stretch;
    background: var(--bg-controls);
    border: 1px solid var(--border-input);
    border-radius: 6px;
    font-size: 12px;
    line-height: 1.6;
}

.panel-title {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-muted);
    margin-bottom: 2px;
}

.sasa-row {
    display: flex;
    gap: 8px;
    justify-content: space-between;
}

.sasa-label {
    color: var(--text-secondary);
    font-weight: 500;
}

.sasa-diff-good { color: var(--sasa-good); font-weight: 600; }
.sasa-diff-warn { color: var(--sasa-warn); font-weight: 600; }
.sasa-diff-bad  { color: var(--sasa-bad);  font-weight: 600; }

/* ================================
   DOC PAGE TABLES + REFS
================================ */

.doc-content table {
    border-collapse: collapse;
    width: 100%;
    font-size: 13px;
    margin-bottom: 16px;
}

.doc-content th,
.doc-content td {
    text-align: left;
    padding: 7px 12px;
    border: 1px solid var(--border-light);
}

.doc-content thead tr {
    background: var(--bg-hover);
    font-weight: 600;
}

.ref-list {
    list-style: none;
    padding: 0;
    margin: 0 0 16px 0;
}

/* ================================
   PASTE MAPPING DIALOG
================================ */

#paste-mapping-dialog {
    border: none;
    border-radius: 10px;
    padding: 0;
    width: min(560px, 92vw);
    box-shadow: 0 8px 32px rgba(0,0,0,0.22);
    background: var(--bg-surface);
    color: var(--text-primary);
    overflow: hidden;
}

#paste-mapping-dialog::backdrop {
    background: rgba(0,0,0,0.4);
}

.paste-dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 13px 16px 11px;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 14px;
}

.paste-dialog-close {
    font-size: 18px;
    line-height: 1;
    width: 26px;
    height: 26px;
    padding: 0;
    border: none;
    background: none;
    cursor: pointer;
    color: var(--text-muted);
    border-radius: 4px;
}

.paste-dialog-close:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

#mapping-paste-area {
    display: block;
    width: 100%;
    box-sizing: border-box;
    padding: 12px 16px;
    border: none;
    border-bottom: 1px solid var(--border);
    resize: vertical;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 12px;
    line-height: 1.55;
    background: var(--bg-base);
    color: var(--text-primary);
    min-height: 200px;
    outline: none;
}

.paste-dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 10px 16px;
}

.confirm-dialog-msg {
    margin: 0;
    padding: 16px 16px 12px;
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.btn-danger {
    background: #dc2626;
    color: white;
    border-color: #dc2626;
}

.btn-danger:hover {
    filter: brightness(1.1);
    background: #dc2626;
}

.btn-success {
    background: #16a34a;
    color: white;
    border-color: #16a34a;
}

.btn-success:hover {
    filter: brightness(1.1);
    background: #16a34a;
}

.btn-success:disabled {
    opacity: 0.5;
}

.btn-primary {
    background: var(--color-primary);
    color: white;
    border-color: var(--color-primary);
}

.btn-primary:hover {
    filter: brightness(1.1);
    background: var(--color-primary);
}

.ref-list li {
    padding: 12px 14px;
    margin-bottom: 8px;
    background: var(--bg-ref);
    border: 1px solid var(--border-light);
    border-radius: 6px;
    font-size: 13px;
    line-height: 1.6;
}
