:root {
  --bg: #0b0f14;
  --surface: #121821;
  --surface-2: #19212c;
  --border: #243040;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #4c8dff;
  --up: #1fbf75;
  --up-bg: rgba(31, 191, 117, .12);
  --down: #f0525b;
  --down-bg: rgba(240, 82, 91, .12);
  --warn: #e8b339;
  --radius: 10px;
  --font: 'Inter', system-ui, -apple-system, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #f0f3f7;
    --border: #dde3ea;
    --text: #111827;
    --muted: #64748b;
    --up: #0f9d58;
    --up-bg: rgba(15, 157, 88, .10);
    --down: #d93743;
    --down-bg: rgba(217, 55, 67, .10);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 17px; }
h2 { font-size: 14px; letter-spacing: .02em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.up { color: var(--up); }
.down { color: var(--down); }

/* --- Barra superior --- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 14px 24px; border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand p { margin: 0; font-size: 12px; }
.brand-mark { color: var(--accent); font-size: 22px; }
.status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 12px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface-2);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill.ok .dot { background: var(--up); box-shadow: 0 0 0 3px var(--up-bg); }
.pill.warn .dot { background: var(--warn); }
.pill.err .dot { background: var(--down); }

/* --- Layout --- */
.layout {
  display: grid; gap: 16px; padding: 16px 24px 40px; max-width: 1440px; margin: 0 auto;
  grid-template-columns: minmax(0, 1fr) 360px;
  grid-template-areas:
    "summary summary"
    "chart   ticket"
    "watch   ticket"
    "blotter blotter"
    "settings settings";
}
.summary { grid-area: summary; display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.chart-card { grid-area: chart; min-width: 0; }
.watchlist { grid-area: watch; }
.ticket { grid-area: ticket; align-self: start; position: sticky; top: 84px; }
.blotter { grid-area: blotter; }
.settings { grid-area: settings; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.card-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.stat { padding: 14px 16px; display: flex; flex-direction: column; gap: 2px; }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 22px; font-weight: 500; }
.stat-sub { font-size: 12px; min-height: 1.4em; }

/* --- Tablas --- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 16px; text-align: left; white-space: nowrap; }
th { font-size: 11px; font-weight: 500; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; border-bottom: 1px solid var(--border); }
tbody tr + tr td { border-top: 1px solid var(--border); }
tbody tr:hover { background: var(--surface-2); }
.num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.actions-col, td.actions { text-align: right; }
td.actions { display: flex; gap: 6px; justify-content: flex-end; }
.sym { font-weight: 600; }
.sym-name { display: block; font-size: 12px; color: var(--muted); max-width: 220px; overflow: hidden; text-overflow: ellipsis; }
.empty { text-align: center; color: var(--muted); padding: 28px 16px; }
.tag { display: inline-block; padding: 1px 7px; border-radius: 4px; font-size: 11px; font-weight: 600; }
.tag.buy { background: var(--up-bg); color: var(--up); }
.tag.sell { background: var(--down-bg); color: var(--down); }
.tag.ext { background: var(--surface-2); color: var(--warn); font-weight: 500; margin-left: 4px; }
.flash-up { animation: flashUp .9s ease-out; }
.flash-down { animation: flashDown .9s ease-out; }
@keyframes flashUp { from { background: var(--up-bg); } to { background: transparent; } }
@keyframes flashDown { from { background: var(--down-bg); } to { background: transparent; } }

/* --- Controles --- */
input {
  width: 100%; padding: 8px 10px; border-radius: 7px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font: 14px var(--mono);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 7px; border: 1px solid transparent;
  font: 600 13px var(--font); cursor: pointer; color: var(--text); background: var(--surface-2);
}
.btn:hover { filter: brightness(1.12); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-ghost { border-color: var(--border); background: transparent; }
.btn-buy { background: var(--up); color: #fff; }
.btn-sell { background: var(--down); color: #fff; }
.btn-danger { background: var(--down-bg); color: var(--down); border-color: var(--down); }
.btn-block { width: 100%; padding: 11px; font-size: 14px; }
.btn-xs { padding: 4px 9px; font-size: 12px; }
.btn-xs.buy { background: var(--up-bg); color: var(--up); }
.btn-xs.sell { background: var(--down-bg); color: var(--down); }
.icon-btn { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 16px; padding: 2px 6px; }
.icon-btn:hover { color: var(--down); }

.inline-form { display: flex; gap: 8px; }
.inline-form input { width: 200px; }

#order-form { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.field em { font-style: normal; opacity: .7; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.segmented { display: flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 3px; }
.segmented button {
  flex: 1; padding: 7px 8px; border: 0; border-radius: 6px; background: transparent;
  color: var(--muted); font: 600 13px var(--font); cursor: pointer;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.25); }
.segmented.side button.active[data-side="buy"] { background: var(--up); color: #fff; }
.segmented.side button.active[data-side="sell"] { background: var(--down); color: #fff; }
.quote-line { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; margin-top: -6px; }
.quote-line .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.estimate { margin: 0; padding: 10px 12px; background: var(--surface-2); border-radius: 8px; display: grid; gap: 4px; }
.estimate div { display: flex; justify-content: space-between; }
.estimate dt { color: var(--muted); font-size: 12px; }
.estimate dd { margin: 0; }
.hint { font-size: 12px; margin: -4px 0 0; min-height: 1em; }

.tabs { justify-content: flex-start; gap: 4px; padding: 6px 10px; }
.tabs button {
  border: 0; background: none; color: var(--muted); font: 600 13px var(--font);
  padding: 8px 12px; border-radius: 6px; cursor: pointer;
}
.tabs button.active { color: var(--text); background: var(--surface-2); }
.count { font-family: var(--mono); font-size: 11px; padding: 0 6px; border-radius: 999px; background: var(--border); margin-left: 4px; }

.settings-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 12px 16px 0; }
.settings .small { padding: 12px 16px 14px; margin: 0; }
.spacer { flex: 1; }
.inline-form input[name="balance"] { width: 140px; }
.check { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.check input { width: auto; }
.file-btn { cursor: pointer; }

/* --- Idioma --- */
.segmented.lang { padding: 2px; }
.segmented.lang button { padding: 3px 9px; font-size: 12px; }

/* --- Gráfico --- */
.chart-head { align-items: flex-start; }
.chart-title { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.chart-sym { font-size: 18px; margin-right: 6px; }
.chart-quote { display: flex; align-items: baseline; gap: 10px; }
.chart-price { font-size: 22px; font-weight: 500; }
.segmented.ranges button { padding: 5px 10px; font-size: 12px; flex: none; }
.chart-wrap { position: relative; height: 380px; }
#chart { position: absolute; inset: 0; }
.chart-msg {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: var(--surface); text-align: center; padding: 16px;
}
.chart-msg[hidden] { display: none; }
.chart-legend { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; padding: 8px 16px 12px; border-top: 1px solid var(--border); }
.chart-legend > span { display: inline-flex; align-items: center; gap: 6px; }
.lg { display: inline-block; width: 16px; height: 0; border-top: 2px solid; }
.lg.entry { border-color: var(--accent); }
.lg.sl { border-color: var(--down); border-top-style: dashed; }
.lg.tp { border-color: var(--up); border-top-style: dashed; }
.lg.order { border-color: var(--muted); border-top-style: dotted; }
tr.selectable { cursor: pointer; }
tr.selected td:first-child { box-shadow: inset 3px 0 0 var(--accent); }

/* --- Diálogo --- */
dialog {
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text);
  padding: 20px; width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog p { margin: 0; font-size: 13px; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 12px; margin: 0; display: flex; flex-direction: column; gap: 10px; }
legend { font-size: 12px; color: var(--muted); padding: 0 6px; }
.dialog-actions { display: flex; justify-content: flex-end; }

/* --- Toasts --- */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 20; max-width: min(380px, calc(100vw - 32px)); }
.toast {
  padding: 10px 14px; border-radius: 8px; background: var(--surface); border: 1px solid var(--border);
  border-left: 4px solid var(--accent); box-shadow: 0 6px 20px rgba(0,0,0,.3); font-size: 13px;
  animation: toastIn .2s ease-out;
}
.toast.ok { border-left-color: var(--up); }
.toast.error { border-left-color: var(--down); }
@keyframes toastIn { from { transform: translateY(8px); opacity: 0; } }

/* --- Responsive --- */
@media (max-width: 1000px) {
  .layout { grid-template-columns: 1fr; grid-template-areas: "summary" "chart" "ticket" "watch" "blotter" "settings"; }
  .ticket { position: static; }
  .chart-wrap { height: 300px; }
  .summary { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 560px) {
  .topbar, .layout { padding-left: 16px; padding-right: 16px; }
  .summary { gap: 10px; }
  .stat-value { font-size: 18px; }
  .inline-form input { width: 100%; }
  .inline-form { width: 100%; }
}

/* Símbolos clickeables: cambian el gráfico */
.selectable .sym, .sym.link { cursor: pointer; }
.selectable:hover .sym, .sym.link:hover { color: var(--accent); text-decoration: underline; }

/* Botones de cotización (watchlist): vender al bid / comprar al ask */
.quote-btn {
  display: inline-flex; flex-direction: column; align-items: flex-end; min-width: 84px;
  padding: 5px 10px; border-radius: 6px; border: 1px solid transparent; cursor: pointer;
  font: 600 14px var(--mono); line-height: 1.2;
}
.quote-btn small { font: 400 11px var(--mono); opacity: .75; min-height: 1em; }
.quote-btn.sell { background: var(--down-bg); color: var(--down); }
.quote-btn.buy { background: var(--up-bg); color: var(--up); }
.quote-btn:hover { border-color: currentColor; }
.btn:disabled:hover { filter: none; }
