:root {
  --bg: #0d0e16;
  --panel: #151724;
  --panel-2: #1e2031;
  --panel-3: #272a40;
  --border: #4e5577;
  --border-soft: #30354d;
  --text: #f1f3ff;
  --muted: #9ca5c8;
  --accent: #2962ff;
  --accent-hover: #3c70ff;
  --danger: #d84c5b;
  --green: #26a69a;
  --red: #ef5350;
  --shadow: 0 14px 34px rgba(0, 0, 0, .34);
  --radius: 10px;
  --header-h: 54px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="light"] {
  --bg: #e9edf5;
  --panel: #ffffff;
  --panel-2: #f4f6fb;
  --panel-3: #e8ecf5;
  --border: #bec6db;
  --border-soft: #d8deeb;
  --text: #1c2435;
  --muted: #68728c;
  --shadow: 0 12px 28px rgba(26, 38, 66, .15);
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; }
body { background: var(--bg); color: var(--text); }
button, select, input { font: inherit; }
button { cursor: pointer; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid #7da0ff; outline-offset: 1px; }

.app-shell { height: 100%; display: grid; grid-template-rows: var(--header-h) 44px minmax(0, 1fr); }
.app-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--border-soft); }
.brand-block { display: flex; align-items: center; gap: 9px; min-width: max-content; }
.brand-logo { width: 38px; height: 38px; object-fit: contain; border-radius: 8px; background: #ffffff; padding: 2px; }
.brand-title { font-size: 14px; font-weight: 800; letter-spacing: .2px; }
.brand-subtitle { color: var(--muted); font-size: 11px; }
.header-controls { display: flex; align-items: center; justify-content: flex-end; gap: 6px; min-width: 0; }
.control-field { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 11px; font-weight: 700; }
.control-field select, .mini-select, .layout-select, .layout-name { height: 30px; color: var(--text); background: var(--panel-2); border: 1px solid var(--border); border-radius: 7px; padding: 0 8px; }
.control-field select { min-width: 105px; }
.status-pill { min-width: 88px; height: 29px; display: inline-flex; align-items: center; justify-content: center; padding: 0 9px; border-radius: 999px; border: 1px solid var(--border); background: var(--panel-2); font-size: 11px; font-weight: 800; }
.status-pill.ok { color: #72e0b8; border-color: rgba(38, 166, 154, .7); }
.status-pill.error { color: #ff9ca6; border-color: rgba(239, 83, 80, .65); }
.status-pill.muted { color: var(--muted); }

.primary-button, .toolbar-button, .danger-button, .tiny-button, .tool-button, .color-button, .open-main-button {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text); border-radius: 7px; font-weight: 800; transition: background .12s ease, border-color .12s ease, transform .12s ease;
}
.primary-button, .toolbar-button, .danger-button { height: 30px; padding: 0 10px; }
.primary-button { background: var(--accent); border-color: #7091ff; color: #fff; }
.primary-button:hover { background: var(--accent-hover); }
.toolbar-button:hover, .tool-button:hover, .open-main-button:hover, .color-button:hover { background: var(--panel-3); border-color: #7d8ec8; }
.danger-button { color: #ffb0b7; border-color: #985461; background: rgba(126, 38, 50, .2); }
.danger-button:hover { background: #742e3a; color: #fff; }

.drawing-toolbar { display: flex; align-items: center; gap: 6px; padding: 4px 8px; background: var(--panel); border-bottom: 1px solid var(--border-soft); overflow: auto hidden; scrollbar-width: none; }
.drawing-toolbar::-webkit-scrollbar { display: none; }
.toolbar-group { height: 34px; display: inline-flex; align-items: center; gap: 3px; flex: 0 0 auto; padding: 3px; border: 1px solid var(--border); border-radius: 9px; background: var(--panel-2); }
.tool-button { min-width: 52px; height: 26px; padding: 0 8px; font-size: 11px; }
.tool-button.active { background: var(--accent); border-color: #8cabff; color: #fff; }
.color-button { height: 26px; display: inline-flex; align-items: center; gap: 5px; padding: 0 7px; font-size: 11px; }
.color-button input { width: 17px; height: 17px; padding: 0; border: 0; background: transparent; }
.mini-select { height: 26px; font-size: 11px; }
.layout-group { margin-left: auto; }
.layout-name { width: 105px; height: 26px; font-size: 11px; font-weight: 700; }
.layout-select { width: 110px; height: 26px; font-size: 11px; }
.layout-group .toolbar-button, .layout-group .danger-button { height: 26px; padding: 0 8px; font-size: 11px; }

.workspace { min-height: 0; display: grid; grid-template-rows: minmax(0, 1fr) auto; }
.charts-grid { min-height: 0; display: grid; grid-template-columns: minmax(250px, 29%) minmax(0, 1fr); gap: 5px; padding: 5px; }
.side-charts { min-height: 0; display: grid; grid-template-rows: repeat(3, minmax(0, 1fr)); gap: 5px; }
.chart-card { min-height: 0; position: relative; display: grid; grid-template-rows: 30px minmax(0, 1fr); background: var(--panel); border: 1px solid var(--border-soft); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.chart-card-header { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 0 8px; background: var(--panel-2); border-bottom: 1px solid var(--border-soft); font-size: 11px; }
.chart-symbol { font-weight: 900; margin-right: 7px; }
.chart-timeframe { color: #82a0ff; font-weight: 900; }
.chart-info { color: var(--muted); }
.chart-header-actions { display: flex; align-items: center; gap: 8px; }
.tiny-button, .open-main-button { height: 22px; padding: 0 7px; font-size: 10px; }
.chart-host { min-width: 0; min-height: 0; position: relative; background: var(--panel); }
.chart-host canvas { user-select: none; }
.drawing-overlay { position: absolute; inset: 0; z-index: 8; width: 100%; height: 100%; touch-action: none; }
.main-chart-host { cursor: crosshair; }
.drop-overlay { position: absolute; inset: 12%; z-index: 15; display: none; align-items: center; justify-content: center; flex-direction: column; gap: 6px; border: 1px dashed #7180b5; border-radius: 12px; background: rgba(17, 19, 31, .84); color: var(--muted); pointer-events: none; }
.drop-overlay.visible { display: flex; }
.drop-overlay.dragging { border-color: #85a4ff; background: rgba(28, 45, 92, .88); color: #fff; }
.drop-icon { width: 48px; height: 48px; display: grid; place-items: center; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 900; }
.chart-toast { position: absolute; top: 40px; left: 12px; z-index: 30; display: none; padding: 6px 9px; border-radius: 7px; background: rgba(14, 16, 28, .9); border: 1px solid #606b96; color: #fff; font-size: 11px; font-weight: 800; pointer-events: none; box-shadow: var(--shadow); }
.chart-toast.visible { display: block; }

.terminal-panel { max-height: 220px; background: var(--panel); border-top: 1px solid var(--border-soft); }
.terminal-panel.collapsed .terminal-body { display: none; }
.terminal-header { height: 32px; display: flex; align-items: center; justify-content: space-between; padding: 0 7px; background: var(--panel-2); }
.terminal-tabs { display: flex; height: 100%; }
.terminal-tab { min-width: 76px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); font-size: 11px; font-weight: 800; }
.terminal-tab.active { color: var(--text); border-bottom-color: var(--accent); }
.terminal-body { height: 145px; overflow: auto; }
.terminal-page { display: none; padding: 8px; }
.terminal-page.active { display: block; }
.data-table { width: 100%; border-collapse: collapse; font-size: 11px; }
.data-table th, .data-table td { padding: 5px 8px; text-align: left; border-bottom: 1px solid var(--border-soft); white-space: nowrap; }
.data-table th { position: sticky; top: 0; background: var(--panel); color: var(--muted); }
.empty-cell { text-align: center !important; color: var(--muted); }
.shortcut-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.shortcut-grid > div { display: flex; align-items: center; gap: 8px; padding: 8px; border: 1px solid var(--border-soft); border-radius: 8px; background: var(--panel-2); font-size: 11px; }
kbd { min-width: 38px; display: inline-flex; align-items: center; justify-content: center; padding: 3px 6px; border: 1px solid var(--border); border-radius: 5px; background: var(--panel-3); box-shadow: inset 0 -1px rgba(255,255,255,.08); font-weight: 900; }
.attribution, .attribution a { color: var(--muted); }

.settings-drawer { position: fixed; inset: 0; z-index: 100; display: none; }
.settings-drawer.open { display: block; }
.settings-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.45); }
.settings-panel { position: absolute; top: 0; right: 0; width: min(390px, 92vw); height: 100%; padding: 14px; background: var(--panel); border-left: 1px solid var(--border); box-shadow: -20px 0 50px rgba(0,0,0,.32); }
.settings-panel header, .help-dialog header { display: flex; align-items: center; justify-content: space-between; }
.settings-panel h2, .help-dialog h2 { margin: 0; font-size: 18px; }
.close-button { width: 30px; height: 30px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2); color: var(--text); font-size: 20px; }
.settings-row { min-height: 48px; display: flex; align-items: center; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--border-soft); font-size: 12px; font-weight: 700; }
.settings-row select { min-width: 125px; height: 30px; border: 1px solid var(--border); border-radius: 7px; color: var(--text); background: var(--panel-2); }
.danger-wide { width: 100%; height: 38px; margin-top: 20px; border: 1px solid #9b5360; border-radius: 8px; background: rgba(128, 38, 51, .24); color: #ffb5bb; font-weight: 900; }
.settings-note { color: var(--muted); font-size: 11px; line-height: 1.5; }
.help-dialog { width: min(620px, 92vw); border: 1px solid var(--border); border-radius: 12px; color: var(--text); background: var(--panel); box-shadow: var(--shadow); }
.help-dialog::backdrop { background: rgba(0,0,0,.55); }
.help-dialog form { margin: 0; }
.help-dialog h3 { margin: 16px 0 5px; font-size: 13px; }
.help-dialog p { color: var(--muted); line-height: 1.55; }
.help-dialog code { color: #9bb3ff; }

.drawing-context-menu { position: fixed; z-index: 80; display: none; min-width: 126px; padding: 5px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel-2); box-shadow: var(--shadow); }
.drawing-context-menu button { width: 100%; height: 28px; border: 0; border-radius: 6px; background: transparent; color: var(--text); text-align: left; padding: 0 8px; font-size: 11px; font-weight: 800; }
.drawing-context-menu button:hover { background: var(--accent); color: #fff; }

@media (max-width: 1050px) {
  .brand-subtitle, .control-field span, #reloadManifestButton { display: none; }
  .charts-grid { grid-template-columns: 1fr; }
  .side-charts { display: none; }
  .layout-group { margin-left: 0; }
}

@media (max-width: 720px) {
  html, body { overflow: auto; }
  .app-shell { min-height: 100vh; height: auto; grid-template-rows: auto auto minmax(700px, 1fr); }
  .app-header { flex-wrap: wrap; }
  .header-controls { width: 100%; justify-content: flex-start; flex-wrap: wrap; }
  .drawing-toolbar { align-items: flex-start; }
  .workspace { min-height: 700px; }
  .shortcut-grid { grid-template-columns: 1fr; }
}
