html, body { height: 100%; overflow: hidden; }
body { display: flex; flex-direction: column; }
.muted { color: var(--text-muted); }
.spacer { flex: 1; }

/* ---------- top bar ---------- */
.topbar {
  height: 56px; flex: none; display: flex; align-items: center; gap: 8px; padding: 0 12px 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 600; white-space: nowrap; }
.brand svg { width: 28px; height: 28px; }
.topbar .sep { color: var(--border); }
.topbar h1 { font-size: 15px; font-weight: 600; margin: 0; white-space: nowrap; }
.project-name {
  margin-left: 8px; height: 30px; min-width: 120px; width: 240px; border: 1px solid transparent; border-radius: 6px;
  background: transparent; color: var(--text-muted); font: 13px var(--font-ui); padding: 0 8px;
}
.project-name:hover, .project-name:focus { border-color: var(--border); background: var(--surface); color: var(--text); }
.save-state { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.menu { position: relative; }
.menu-list {
  position: absolute; top: 34px; left: 0; z-index: 50; min-width: 240px; padding: 6px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); display: flex; flex-direction: column;
}
.menu-list.right { left: auto; right: 0; }
.menu-list[hidden] { display: none; }
.menu-list button {
  text-align: left; border: 0; background: none; color: var(--text); font: 13px var(--font-ui); padding: 8px 10px; border-radius: 5px; cursor: pointer;
}
.menu-list button:hover { background: var(--surface-2); }
.menu-list hr { border: 0; border-top: 1px solid var(--border); margin: 4px 0; width: 100%; }
#sidebarBtn, #readoutsBtn { display: none; }
#readoutsBtn { position: relative; }
#readoutsBtn[data-count]::after { content: attr(data-count); position: absolute; top: 0; right: 0; min-width: 15px; height: 15px; border-radius: 8px; background: var(--danger); color: #fff; font: 600 10px/15px var(--font-ui); padding: 0 3px; }

/* ---------- workspace ---------- */
.workspace { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 316px; flex: none; overflow-y: auto; background: var(--surface); border-right: 1px solid var(--border);
  scrollbar-width: thin;
}
.readouts { width: 240px; flex: none; overflow-y: auto; background: var(--surface); border-left: 1px solid var(--border); padding: 14px 16px; }
.viewport { flex: 1; min-width: 0; display: flex; flex-direction: column; }

/* accordions */
.acc { border-bottom: 1px solid var(--border); }
.acc > summary {
  list-style: none; cursor: pointer; display: flex; align-items: baseline; gap: 8px; padding: 12px 16px; font-weight: 600; user-select: none;
}
.acc > summary::-webkit-details-marker { display: none; }
.acc > summary::before { content: '▸'; color: var(--text-muted); font-size: 11px; width: 10px; transition: transform .15s; }
.acc[open] > summary::before { transform: rotate(90deg); }
.acc > summary .sum { font-weight: 400; color: var(--text-muted); font-size: 12px; margin-left: auto; font-family: var(--font-mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.acc > summary:hover { background: var(--surface-2); }
.acc-body { padding: 4px 16px 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px 10px; }
.acc-body .full { grid-column: 1 / -1; }

.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field > label { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.input { position: relative; display: flex; align-items: center; }
.input input, .input select, .foil-btn {
  width: 100%; height: 34px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text);
  padding: 0 8px; font-size: 13px; min-width: 0;
}
.input input:hover, .input select:hover, .foil-btn:hover { border-color: var(--text-muted); }
.input input:focus, .input select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.input .unit { position: absolute; right: 8px; font: 11px var(--font-mono); color: var(--text-muted); pointer-events: none; }
.input input[type="number"] { padding-right: 30px; -moz-appearance: textfield; }
.input input::-webkit-inner-spin-button, .input input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.input input.invalid { border-color: var(--danger); }
.chk { display: flex; align-items: center; gap: 6px; font-size: 13px; cursor: pointer; user-select: none; white-space: nowrap; }
.chk input { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }
.foil-btn { display: flex; align-items: center; gap: 8px; text-align: left; cursor: pointer; font-family: var(--font-ui); }
.foil-btn canvas { width: 56px; height: 20px; flex: none; }
.foil-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.panel-card, .spar-card {
  grid-column: 1 / -1; border: 1px solid var(--border); border-radius: 6px; padding: 10px; display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px;
  background: var(--background);
}
.spar-card { grid-template-columns: 1fr 1fr 1fr; }
.card-head { grid-column: 1 / -1; display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: 13px; }
.card-head .x { margin-left: auto; }
.card-head .tag { font: 11px var(--font-mono); color: var(--text-muted); font-weight: 400; }
.panel-card .wide, .spar-card .wide { grid-column: span 2; }
.panel-card .full, .spar-card .full { grid-column: 1 / -1; }
.add-row { grid-column: 1 / -1; display: flex; gap: 8px; }

/* readouts */
.readouts h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); margin: 0 0 8px; font-weight: 600; }
.readouts dl { margin: 0 0 18px; display: grid; grid-template-columns: 1fr auto; gap: 5px 8px; font-size: 13px; }
.readouts dt { color: var(--text-muted); }
.readouts dd { margin: 0; text-align: right; font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.readouts dd small { color: var(--text-muted); }
.warn-list { margin: 0 0 16px; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.warn-list li { font-size: 12px; padding: 6px 8px; border-radius: 6px; border: 1px solid var(--warn); color: var(--warn); }
.warn-list li::before { content: '⚠ '; }

/* tabs & panes */
.tabs { height: 40px; flex: none; display: flex; gap: 2px; padding: 0 12px; border-bottom: 1px solid var(--border); background: var(--surface); overflow-x: auto; }
.tabs button {
  border: 0; background: none; color: var(--text-muted); font: 500 13px var(--font-ui); padding: 0 14px; cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tabs button:hover { color: var(--text); background: var(--surface-2); }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.pane { display: none; flex: 1; min-height: 0; flex-direction: column; }
.pane.active { display: flex; }
.pane-tools { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; min-height: 48px; }
.pane-tools select { height: 28px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); font: 12px var(--font-mono); padding: 0 6px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; border-right: 1px solid var(--border); background: var(--surface); color: var(--text); font: 12px var(--font-ui); padding: 0 10px; height: 28px; cursor: pointer; }
.seg button:last-child { border-right: 0; }
.seg button:hover { background: var(--surface-2); }
.seg button.on { background: var(--accent); color: var(--on-accent); }
.toggles { display: flex; gap: 12px; flex-wrap: wrap; }
.canvas-wrap { position: relative; flex: 1; min-height: 0; background: var(--background); overflow: hidden; }
.canvas-wrap canvas { display: block; width: 100%; height: 100%; }
.hint { position: absolute; left: 12px; bottom: 10px; font-size: 12px; color: var(--text-muted); pointer-events: none; }

/* sheets */
.sheets-scroll { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 18px; }
.sheet-block h4 { margin: 0 0 6px; font-size: 13px; font-weight: 600; display: flex; gap: 10px; align-items: center; }
.sheet-block h4 span { font: 12px var(--font-mono); color: var(--text-muted); font-weight: 400; }
.sheet-canvas { overflow-x: auto; }
.sheet-block canvas { display: block; border-radius: 4px; }

/* aero */
.aero-scroll { flex: 1; overflow: auto; padding: 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 10px; margin-bottom: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.card .k { font-size: 12px; color: var(--text-muted); }
.card .v { font: 500 20px var(--font-mono); font-variant-numeric: tabular-nums; margin-top: 2px; }
.card .v small { font-size: 12px; color: var(--text-muted); font-weight: 400; }
.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 14px; }
.chart { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 6px; position: relative; }
.chart h4 { margin: 0 0 4px; font-size: 13px; font-weight: 600; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart .tip { position: absolute; pointer-events: none; background: var(--text); color: var(--background); font: 11px var(--font-mono); padding: 4px 6px; border-radius: 4px; white-space: nowrap; display: none; z-index: 5; }
.chart .legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-muted); flex-wrap: wrap; }
.chart .legend i { display: inline-block; width: 16px; height: 0; border-top: 2px solid; vertical-align: middle; margin-right: 5px; }
.note { font-size: 12px; color: var(--text-muted); max-width: 900px; line-height: 1.5; }

/* airfoil browser */
.airfoil-browser { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(280px, 380px) 1fr; }
.foil-list-wrap { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); }
.foil-search { padding: 10px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--border); }
.foil-search input[type="search"] { height: 34px; border: 1px solid var(--border); border-radius: 6px; padding: 0 10px; background: var(--surface); color: var(--text); font: 13px var(--font-ui); }
.foil-search .row { display: flex; gap: 8px; align-items: center; font-size: 12px; color: var(--text-muted); }
.foil-search .row input { width: 56px; height: 26px; border: 1px solid var(--border); border-radius: 5px; background: var(--surface); color: var(--text); padding: 0 6px; font: 12px var(--font-mono); }
.foil-list { flex: 1; overflow-y: auto; }
.foil-item { display: grid; grid-template-columns: 84px 1fr; gap: 2px 10px; padding: 7px 10px; border-bottom: 1px solid var(--border); cursor: pointer; align-items: center; }
.foil-item:hover { background: var(--surface-2); }
.foil-item.sel { background: color-mix(in srgb, var(--accent) 14%, transparent); box-shadow: inset 3px 0 0 var(--accent); }
.foil-item canvas { width: 84px; height: 30px; grid-row: span 2; }
.foil-item b { font: 500 12px var(--font-mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.foil-item span { font-size: 11px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.foil-detail { overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.foil-detail h2 { margin: 0; font-size: 18px; }
.foil-detail .big { width: 100%; height: 200px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.foil-detail .actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.foil-detail .stats { display: flex; gap: 18px; flex-wrap: wrap; font-size: 13px; }
.foil-detail .stats b { font-family: var(--font-mono); font-weight: 500; }
.foil-detail .row2 { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 14px; }

.foil-dialog { width: min(920px, 94vw); height: min(680px, 88vh); padding: 0; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.foil-dialog[open] { display: flex; flex-direction: column; }
.foil-dialog::backdrop { background: #0006; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; border-bottom: 1px solid var(--border); }
#foilPicker { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(260px, 360px) 1fr; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%) translateY(80px); background: var(--text); color: var(--background); padding: 9px 16px; border-radius: 8px; font-size: 13px; transition: transform .2s; z-index: 100; }
.toast.show { transform: translateX(-50%) translateY(0); }

.busy { position: absolute; inset: 0; display: grid; place-items: center; background: color-mix(in srgb, var(--background) 60%, transparent); font-size: 13px; color: var(--text-muted); }

@media (max-width: 1200px) {
  #readoutsBtn { display: inline-flex; }
  .readouts { position: fixed; top: 56px; bottom: 0; right: 0; z-index: 40; transform: translateX(100%); transition: transform .2s; box-shadow: var(--shadow); width: 260px; }
  .readouts.open { transform: none; }
}
@media (max-width: 900px) {
  #sidebarBtn { display: inline-flex; }
  .sidebar { position: fixed; top: 56px; bottom: 0; left: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .project-name, .save-state, .topbar .sep, .topbar h1 { display: none; }
  .airfoil-browser, #foilPicker { grid-template-columns: 1fr; grid-template-rows: 45% 55%; }
  .foil-list-wrap { border-right: 0; border-bottom: 1px solid var(--border); }
  .charts { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .brand span { display: none; }
  .topbar { gap: 2px; padding: 0 6px; }
  .topbar a.btn, #undoBtn, #redoBtn, #themeBtn { display: none; }
  .topbar .btn.sm { padding: 0 8px; }
}
