:root {
  /* MTT-Flow-Classic theme (desktop) */
  --bg: #c0c0c0;
  --card: #c0c0c0;
  --text: #000000;
  --muted: #464646;
  --accent: #464646;
  --ok: #007800;
  --err: #c80000;
  --border: #646464;
  --chart-bg: #1e5082;
  --panel-bg: #b8b8b8;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.9rem; }
.err { color: var(--err); margin-top: 10px; }

.panel {
  max-width: 420px;
  margin: 10vh auto;
  background: #d4d4d4;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 28px;
}
.login-panel h1 {
  margin: 0;
  font-size: 1.5rem;
  line-height: 1.2;
  display: inline-block;
  width: auto;
}
.login-brand {
  display: flex !important;
  flex-direction: row !important;
  flex-wrap: nowrap !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 12px;
  margin: 0 0 12px;
  width: 100%;
  text-align: center;
}
.login-logo {
  width: 48px !important;
  height: 48px !important;
  border-radius: 10px;
  object-fit: cover;
  flex: 0 0 48px;
  display: block;
}
.login-subtitle {
  text-align: center;
  margin: 0 0 8px;
}
.login-panel label { display: block; margin: 12px 0 4px; color: var(--muted); font-size: 0.85rem; }
.login-panel input {
  width: 100%;
  padding: 8px 10px;
  border-radius: 2px;
  border: 1px solid #646464;
  background: #ffffff;
  color: var(--text);
}
.login-panel button {
  margin-top: 16px;
  padding: 6px 14px;
  border: 1px solid #646464;
  border-radius: 2px;
  background: #d4d4d4;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
}
.login-panel button:hover {
  background: #e8e8e8;
}
.toolbar select,
.toolbar input[type="number"],
.toolbar button {
  height: 28px;
  box-sizing: border-box;
  vertical-align: middle;
}
.toolbar button {
  margin: 0;
  padding: 4px 12px;
  border: 1px solid #646464;
  border-radius: 2px;
  background: #d4d4d4;
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  line-height: 1.2;
}
.toolbar button:hover {
  background: #e8e8e8;
}
.toolbar button.secondary {
  background: #d4d4d4;
}
.toolbar button.toggle-on {
  background: #1e5082;
  color: #ffffff;
  border-color: #1e5082;
}
.toolbar button.toggle-on:hover {
  background: #2a6299;
}

#appView { display: flex; flex-direction: column; height: 100%; }
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--card);
  flex-wrap: wrap;
}
.toolbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.toolbar label { font-size: 0.85rem; color: var(--muted); }
.toolbar select {
  padding: 4px 8px;
  border-radius: 2px;
  border: 1px solid #646464;
  background: #ffffff;
  color: var(--text);
  font-size: 0.85rem;
}
.toolbar input[type="number"] {
  width: 3.5rem;
  padding: 4px 6px;
  border-radius: 2px;
  border: 1px solid #646464;
  background: #ffffff;
  color: var(--text);
  font-size: 0.85rem;
}
.toolbar input[type="checkbox"] {
  width: auto;
  margin: 0 2px 0 0;
  vertical-align: middle;
}

.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: stretch;
  gap: 0;
  background: var(--chart-bg);
}

.chart-wrap {
  flex: 1;
  min-width: 0;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--chart-bg);
  border-right: 1px solid var(--border);
}

.chart-wrap #chart {
  flex: 1;
  min-height: 0;
  width: 100%;
}

#fpCanvas {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

.side-panel {
  width: 240px;
  flex-shrink: 0;
  align-self: stretch;
  height: 100%;
  border-left: 1px solid var(--border);
  background: var(--panel-bg);
  display: grid;
  grid-template-columns: minmax(0, 1fr) 0px;
  grid-template-rows: minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}

.side-panel.tas-visible {
  width: 420px;
  grid-template-columns: 240px 180px;
}

.dom-panel {
  grid-column: 1;
  grid-row: 1;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.tas-panel {
  grid-column: 2;
  grid-row: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
  border-left: none;
}

.side-panel.tas-visible .tas-panel {
  opacity: 1;
  pointer-events: auto;
  visibility: visible;
  border-left: 1px solid var(--border);
}

.panel-head {
  padding: 6px 10px;
  font-size: 0.8rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  background: rgba(192, 192, 192, 0.85);
  flex-shrink: 0;
}

.dom-ladder {
  flex: 1;
  min-height: 0;
  min-width: 0;
  width: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: stretch;
  font-family: "Azeret Mono", Consolas, monospace;
  font-size: 0.78rem;
  background: #1e5082;
  color: #fafaff;
}

.dom-row {
  flex: 1 1 18px;
  height: 18px;
  min-height: 14px;
  max-height: 28px;
  min-width: 0;
  position: relative;
  display: grid;
  grid-template-columns: 1fr 0.55fr;
  gap: 6px;
  padding: 2px 8px;
  line-height: 1.35;
  align-content: center;
  overflow: hidden;
  box-sizing: border-box;
}

.dom-empty {
  padding: 12px 10px;
  text-align: center;
  color: #fafaff;
}

.dom-row.ask .dom-price,
.dom-row.ask .dom-qty { color: #dc2828; }
.dom-row.bid .dom-price,
.dom-row.bid .dom-qty { color: #90ee90; }

.dom-qty-bar {
  position: absolute;
  top: 1px;
  bottom: 1px;
  right: 0;
  opacity: 0.22;
  pointer-events: none;
}

.dom-row.ask .dom-qty-bar { background: #dc2828; }
.dom-row.bid .dom-qty-bar { background: #90ee90; }

.dom-price { text-align: right; z-index: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dom-qty { text-align: right; z-index: 1; color: rgba(250, 250, 255, 0.75); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.dom-spread {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  margin: 4px 0;
  background: rgba(0, 0, 0, 0.15);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.82rem;
  color: #fafaff;
}

.dom-spread .bid { color: #90ee90; }
.dom-spread .ask { color: #dc2828; }
.dom-spread .sep { color: rgba(250, 250, 255, 0.6); font-weight: 400; }

.tas-list {
  flex: 1;
  min-height: 0;
  min-width: 0;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  font-family: "Azeret Mono", Consolas, monospace;
  font-size: 0.75rem;
  background: #1e5082;
  color: #fafaff;
}

.tas-row {
  display: grid;
  grid-template-columns: 1fr 1fr 0.6fr 0.4fr;
  gap: 4px;
  padding: 3px 8px;
  border-bottom: 1px solid rgba(100, 100, 100, 0.35);
}

.tas-row.buy .tas-price,
.tas-row.buy .tas-side { color: #90ee90; }
.tas-row.sell .tas-price,
.tas-row.sell .tas-side { color: #dc2828; }
.tas-time { color: rgba(250, 250, 255, 0.65); }

.status-bar {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 12px;
  border-top: 1px solid var(--border);
  background: var(--card);
  font-size: 0.85rem;
}

.pill {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 2px;
  font-size: 0.75rem;
  background: #d4d4d4;
  border: 1px solid #646464;
}
.pill.online { color: var(--ok); }
.pill.offline { color: var(--muted); }
.pill.error { color: var(--err); }

/* --- visibility helpers --- */
.mobile-only { display: none !important; }
.desktop-only { display: revert; }

/* --- mobile drawer --- */
.mobile-drawer {
  position: fixed;
  inset: 0;
  z-index: 1000;
  pointer-events: none;
}
.mobile-drawer:not(.hidden) {
  pointer-events: auto;
}
.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  opacity: 0;
  transition: opacity 0.2s ease;
}
.mobile-drawer.open .drawer-backdrop {
  opacity: 1;
}
.drawer-panel {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(280px, 85vw);
  background: #1a1a1a;
  color: #f0f0f0;
  transform: translateX(-100%);
  transition: transform 0.22s ease;
  overflow-y: auto;
  padding: 12px 14px 24px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.mobile-drawer.open .drawer-panel {
  transform: translateX(0);
}
.drawer-head {
  padding: 8px 4px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  margin-bottom: 4px;
}
.drawer-head strong { display: block; font-size: 1rem; }
.drawer-section-label {
  margin: 10px 0 2px;
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  color: rgba(255, 255, 255, 0.45);
  text-transform: uppercase;
}
.drawer-tool {
  text-align: left;
  padding: 10px 12px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: #f0f0f0;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.drawer-tool.active {
  background: #1e5082;
}
.drawer-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.82rem;
  color: rgba(255, 255, 255, 0.7);
  margin: 4px 0;
}
.drawer-field select,
.drawer-field input[type="number"] {
  padding: 8px 10px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: #2a2a2a;
  color: #fff;
  font-size: 0.9rem;
}
.drawer-check {
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}
.drawer-check input[type="number"] {
  width: 4.5rem;
}
.drawer-action {
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 4px;
  background: #1e5082;
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.drawer-action.secondary {
  background: #333;
}
.drawer-status {
  align-self: flex-start;
  margin-top: 4px;
}

/* --- mobile toolbar --- */
.icon-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #d4d4d4;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}
.toolbar-mobile-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 4px 0;
}
.mobile-bar-title {
  flex: 1;
  min-width: 0;
  line-height: 1.25;
}
.mobile-bar-title strong {
  display: block;
  font-size: 0.95rem;
}
.mobile-bar-title .muted {
  font-size: 0.78rem;
}
.mobile-conn {
  flex-shrink: 0;
  font-size: 0.68rem;
  padding: 2px 6px;
}
.mobile-toggle-row {
  display: flex;
  gap: 6px;
  width: 100%;
  padding: 0 0 4px;
}
.mob-toggle {
  flex: 1;
  min-height: 34px;
  padding: 6px 8px;
  border: 1px solid #646464;
  border-radius: 4px;
  background: #d4d4d4;
  color: var(--text);
  font-weight: 600;
  font-size: 0.82rem;
  cursor: pointer;
}
.mob-toggle.toggle-on {
  background: #1e5082;
  color: #fff;
  border-color: #1e5082;
}

/* --- mobile layout --- */
@media (max-width: 768px),
  (min-width: 769px) and (max-width: 1180px),
  (pointer: coarse) and (max-width: 1366px) {
  body.layout-mobile {
    --mobile-major: 61.8%;
    --mobile-minor: 38.2%;
  }

  body.layout-mobile .mobile-only:not(.hidden) { display: flex !important; }
  body.layout-mobile .mobile-toggle-row.mobile-only:not(.hidden) { display: flex !important; }
  body.layout-mobile .desktop-only { display: none !important; }

  body.layout-mobile .login-panel {
    max-width: none;
    margin: 0;
    min-height: 100%;
    border-radius: 0;
    padding: 20px 16px;
  }

  body.layout-mobile .toolbar {
    flex-direction: column;
    align-items: stretch;
    padding: 6px 8px;
    gap: 4px;
    flex-shrink: 0;
  }

  body.layout-mobile .workspace {
    flex: 1;
    min-height: 0;
    flex-direction: column;
  }

  /* 无辅助面板：主图占满 */
  body.layout-mobile .chart-wrap {
    flex: 1;
    min-height: 0;
    border-right: none;
  }

  body.layout-mobile .side-panel {
    display: none;
    width: 100%;
    min-height: 0;
    overflow: hidden;
    border-left: none;
    background: var(--panel-bg);
  }

  /* 三栏布局：开启 DOM 或 T&S 时激活 */
  body.layout-mobile .workspace.mobile-split .chart-wrap {
    flex: 0 0 var(--mobile-major);
    min-height: 0;
  }

  body.layout-mobile .workspace.mobile-split .side-panel {
    display: grid;
    flex: 1 1 var(--mobile-minor);
    min-height: 0;
    width: auto !important;
    border-top: 1px solid var(--border);
  }

  /* 覆盖桌面端 .side-panel.tas-visible 的 420px 双列布局 */
  body.layout-mobile .side-panel.mobile-split-panel,
  body.layout-mobile .side-panel.mobile-split-panel.tas-visible {
    width: auto !important;
  }

  body.layout-mobile .side-panel.mobile-split-panel .tas-panel {
    grid-column: 1;
    grid-row: 1;
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    border-left: none;
  }

  /* 竖屏：上图 + 下两栏（DOM 左 | T&S 右） */
  body.layout-mobile.orientation-portrait .workspace.mobile-split .side-panel.split-both {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) !important;
  }

  body.layout-mobile.orientation-portrait .workspace.mobile-split .side-panel.split-dom-only,
  body.layout-mobile.orientation-portrait .workspace.mobile-split .side-panel.split-ts-only {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) !important;
  }

  /* 横屏：左图（大）+ 右侧上下（DOM 上 | T&S 下） */
  body.layout-mobile.orientation-landscape .workspace {
    flex-direction: row;
    min-height: 0;
  }

  body.layout-mobile.orientation-landscape .workspace.mobile-split .chart-wrap {
    flex: 0 0 var(--mobile-major);
    min-width: 0;
    border-right: 1px solid var(--border);
    border-top: none;
  }

  body.layout-mobile.orientation-landscape .workspace.mobile-split .side-panel {
    flex: 0 0 var(--mobile-minor);
    height: auto;
    align-self: stretch;
    min-height: 0;
    border-top: none;
    border-left: 1px solid var(--border);
  }

  body.layout-mobile.orientation-landscape .workspace.mobile-split .side-panel.split-both {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1.618fr) minmax(0, 1fr) !important;
  }

  body.layout-mobile.orientation-landscape .workspace.mobile-split .side-panel.split-dom-only,
  body.layout-mobile.orientation-landscape .workspace.mobile-split .side-panel.split-ts-only {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) !important;
  }

  body.layout-mobile .side-panel.mobile-split-panel .dom-panel,
  body.layout-mobile .side-panel.mobile-split-panel .tas-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  body.layout-mobile .side-panel.mobile-split-panel .dom-panel {
    grid-column: 1;
    grid-row: 1;
  }

  body.layout-mobile.orientation-portrait .side-panel.split-both .dom-panel {
    border-right: 1px solid var(--border);
  }

  body.layout-mobile.orientation-portrait .side-panel.split-both {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) !important;
  }

  body.layout-mobile.orientation-portrait .side-panel.split-both .tas-panel {
    grid-column: 2 !important;
    grid-row: 1 !important;
  }

  body.layout-mobile.orientation-portrait .side-panel.split-dom-only,
  body.layout-mobile.orientation-portrait .side-panel.split-ts-only {
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) !important;
  }

  body.layout-mobile.orientation-portrait .side-panel.split-dom-only .tas-panel,
  body.layout-mobile.orientation-portrait .side-panel.split-ts-only .dom-panel {
    display: none !important;
  }

  body.layout-mobile.orientation-landscape .side-panel.split-both .dom-panel {
    grid-column: 1 !important;
    grid-row: 1 !important;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  body.layout-mobile.orientation-landscape .side-panel.split-both .tas-panel {
    grid-column: 1 !important;
    grid-row: 2 !important;
  }

  body.layout-mobile.orientation-landscape .side-panel.split-dom-only .dom-panel,
  body.layout-mobile.orientation-landscape .side-panel.split-ts-only .tas-panel {
    grid-column: 1 !important;
    grid-row: 1 !important;
  }

  body.layout-mobile.orientation-landscape .side-panel.split-dom-only .tas-panel,
  body.layout-mobile.orientation-landscape .side-panel.split-ts-only .dom-panel {
    display: none !important;
  }

  body.layout-mobile .side-panel.mobile-split-panel .dom-ladder,
  body.layout-mobile .side-panel.mobile-split-panel .tas-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  body.layout-mobile .side-panel.mobile-split-panel .dom-ladder {
    display: block;
  }

  body.layout-mobile .side-panel.mobile-split-panel .dom-row {
    flex: none;
    height: 16px;
    min-height: 16px;
    max-height: 16px;
    font-size: 0.72rem;
  }

  body.layout-mobile .side-panel.mobile-split-panel .dom-spread {
    margin: 2px 0;
    padding: 4px 6px;
    font-size: 0.75rem;
  }

  body.layout-mobile .side-panel.mobile-split-panel .panel-head {
    padding: 4px 8px;
    font-size: 0.75rem;
  }

  body.layout-mobile .side-panel.mobile-split-panel .tas-row {
    padding: 2px 6px;
    font-size: 0.7rem;
  }

  body.layout-mobile .status-bar {
    font-size: 0.75rem;
    padding: 4px 8px;
    flex-wrap: wrap;
    flex-shrink: 0;
  }
}

@media (min-width: 769px) {
  body.layout-desktop .mobile-only { display: none !important; }
}

/* --- Pad / tablet tweaks (same mobile chrome, larger touch targets) --- */
body.layout-pad.layout-mobile {
  --mobile-major: 62%;
  --mobile-minor: 38%;
}

body.layout-pad .drawer-panel {
  width: min(340px, 70vw);
  padding: 16px 18px 28px;
}

body.layout-pad .drawer-tool {
  padding: 14px 14px;
  font-size: 1.05rem;
}

body.layout-pad .icon-btn {
  width: 42px;
  height: 42px;
  font-size: 1.25rem;
}

body.layout-pad .mob-toggle {
  min-height: 40px;
  font-size: 0.92rem;
  padding: 8px 10px;
}

body.layout-pad .mobile-bar-title strong {
  font-size: 1.05rem;
}

body.layout-pad .toolbar {
  padding: 8px 12px;
}

body.layout-pad.orientation-landscape .workspace.mobile-split .side-panel {
  flex: 0 0 min(38%, 360px);
}

body.layout-pad.orientation-portrait .workspace.mobile-split .chart-wrap {
  flex: 0 0 58%;
}

body.layout-pad.orientation-portrait .workspace.mobile-split .side-panel {
  flex: 1 1 42%;
  min-height: 240px;
}

body.layout-pad .side-panel.mobile-split-panel .dom-row {
  height: 18px;
  min-height: 18px;
  max-height: 18px;
  font-size: 0.8rem;
}

body.layout-pad .side-panel.mobile-split-panel .tas-row {
  padding: 4px 8px;
  font-size: 0.78rem;
}

body.layout-pad .status-bar {
  font-size: 0.82rem;
  padding: 6px 12px;
}

/* Pad landscape: prefer side-by-side even without split for better use of width */
@media (min-width: 769px) and (max-width: 1180px) and (orientation: landscape) {
  body.layout-pad.layout-mobile .workspace:not(.mobile-split) {
    flex-direction: row;
  }
}
