/* ============================================================================
   RespiraMark 標籤工具 — 主題與版面
   配色完整移植桌面版 ui/theme.py：
     :root                    = 標準佈景（淺色，預設）
     [data-theme="eye_care"]  = 護眼模式（中性炭灰）
   版面對應 ui/forms/GUI_label.ui。
   ============================================================================ */

:root {
  color-scheme: light;
  --window-bg: #F2F4F8;
  --surface: #FFFFFF;
  --border: #E3E7EE;
  --border-strong: #CBD3DE;
  --text: #1F2937;
  --text-2: #6B7280;
  --accent: #2563EB;
  --accent-hover: #1D4ED8;
  --accent-subtle: #EFF4FF;
  --accent-selection: #DBE7FF;
  --disabled-bg: #F3F4F6;
  --disabled-text: #A6ADBB;
  --tooltip-bg: #313743;
  --tooltip-text: #F5F7FA;
  --chip-neutral-bg: #F5F7FB;
  --chip-measured-border: #D7E3FC;
  --table-alt-row: #F8FAFD;
  --table-gridline: #EDF0F5;
  --table-header-text: #4B5563;
  --scrollbar-handle: #C6CDD8;
  --scrollbar-handle-hover: #A9B2C0;
  --wave-bg: #FFFFFF;
  --wave-border: #E3E7EE;
  --cursor-color: #2563EB;
  --overlay-bg: rgba(242, 244, 248, 0.8);
}

[data-theme="eye_care"] {
  color-scheme: dark;
  --window-bg: #1C1C1E;
  --surface: #242426;
  --border: #38383A;
  --border-strong: #4A4A4D;
  --text: #E4E4E6;
  --text-2: #9B9BA1;
  --accent: #4C8DFF;
  --accent-hover: #6BA1FF;
  --accent-subtle: #243352;
  --accent-selection: #2C4570;
  --disabled-bg: #2B2B2D;
  --disabled-text: #6B6B6E;
  --tooltip-bg: #0E0E0F;
  --tooltip-text: #E4E4E6;
  --chip-neutral-bg: #2B2B2E;
  --chip-measured-border: #375079;
  --table-alt-row: #202022;
  --table-gridline: #333335;
  --table-header-text: #B8B8BC;
  --scrollbar-handle: #3D3D40;
  --scrollbar-handle-hover: #505054;
  --wave-bg: #101011;
  --wave-border: #333335;
  --cursor-color: #4C8DFF;
  --overlay-bg: rgba(28, 28, 30, 0.8);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--window-bg);
  color: var(--text);
  font-family: "Microsoft JhengHei UI", "微軟正黑體", "Segoe UI", sans-serif;
  font-size: 13.5px;
  overflow: hidden;
}

.spacer { flex: 1; }

/* 細型捲軸（對應桌面版 QScrollBar 樣式） */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-handle) transparent;
}

/* ---------------------------------------------------------------- 按鈕 --- */

.btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 5px 12px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover:not(:disabled) { background: var(--accent-subtle); border-color: var(--accent); }
.btn:active:not(:disabled) { background: var(--accent-selection); }
.btn:disabled { background: var(--disabled-bg); border-color: var(--border); color: var(--disabled-text); cursor: default; }
.btn.checked { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: bold; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }

/* 圖示按鈕（側欄 📂💾 38 高；↶↷⏭ 34 高，對應 .ui 尺寸） */
.icon-btn, .icon-btn-sm {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.icon-btn { flex: 1; max-width: 130px; height: 38px; }
.icon-btn-sm { width: 38px; height: 34px; flex-shrink: 0; }
.icon-btn svg, .icon-btn-sm svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 3px 8px;
  min-height: 26px;
  font-size: 13px;
  font-family: inherit;
}
.select:focus { outline: none; border-color: var(--accent); }

/* -------------------------------------------------------------- 主版面 --- */

.label-root {
  height: 100%;
  display: flex;
  gap: 12px;
  padding: 12px;
}

.main-col {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* 三軌波形（白底卡片，各自帶邊框，對應桌面版 plot 外框） */
.charts {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.chart-wrap {
  position: relative;
  background: var(--wave-bg);
  border: 1px solid var(--wave-border);
  border-radius: 4px;
  overflow: hidden;
}
.chart-title {
  position: absolute;
  top: 2px;
  left: 66px;
  z-index: 5;
  font-size: 11px;
  color: var(--text-2);
  pointer-events: none;
}

/* uPlot 十字游標：虛線 accent（顏色隨主題） */
.u-cursor-x { border-right: 1px dashed var(--cursor-color) !important; }

/* 狀態列卡片（isCard）：讀值 | 配色 | 視窗尺度 */
.status-card {
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 12px;
  flex-shrink: 0;
}
.lv-stat { font-size: 12.5px; white-space: pre; }
.stat-label { color: var(--text-2); }
.stat-value {
  font-family: Consolas, "Courier New", monospace;
  color: var(--accent);
  font-weight: 600;
}
.stat-value.stat-empty { color: var(--disabled-text); }
.stat-unit { color: var(--text-2); }
.status-select-label {
  color: var(--text);
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

/* ------------------------------------------------------------- 參數列 --- */

.params-row {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
  align-items: stretch;
}

/* QGroupBox 樣式：白卡、圓角、標題浮在左上邊框上 */
.group-box {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 8px 6px;
  margin-top: 8px;
}
.group-title {
  position: absolute;
  top: -9px;
  left: 10px;
  background: var(--surface);
  padding: 0 4px;
  font-size: 12px;
  font-weight: bold;
  color: var(--text-2);
  line-height: 1.4;
  border-radius: 4px;
}
.group-mode {
  min-width: 130px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
}
.group-settings, .group-measured { flex: 1; min-width: 0; }

.param-mode-value {
  font-size: 17px;
  font-weight: bold;
  text-align: center;
}
.param-mode-add {
  font-size: 12.5px;
  color: var(--text-2);
  text-align: center;
}

.param-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px 8px;
}
.param-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  padding: 4px 3px;
  border-radius: 8px;
  line-height: 1.25;
  text-align: center;
}
.chip-setting { background: var(--chip-neutral-bg); border: 1px solid var(--border); }
.chip-measured { background: var(--accent-subtle); border: 1px solid var(--chip-measured-border); }
.chip-label { font-size: 10.5px; color: var(--text-2); }
.chip-value { font-size: 15px; font-weight: 600; }
.chip-value-setting { color: var(--text); }
.chip-value-measured { color: var(--accent); }
.chip-value-empty { color: var(--disabled-text); }
.chip-unit { font-size: 9.5px; color: var(--text-2); }

/* --------------------------------------------------------------- 側欄 --- */

.sidebar {
  width: 300px;
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.side-toolbar { display: flex; gap: 6px; }

.side-fileinfo { display: flex; flex-direction: column; gap: 2px; }
.side-filename {
  color: var(--text-2);
  font-size: 11px;
  text-align: center;
  line-height: 1.4;
  overflow: hidden;
}
.side-filename span {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.side-savestatus {
  font-weight: bold;
  text-align: center;
  font-size: 13px;
  min-height: 18px;
}

.side-progress {
  display: flex;
  align-items: center;
  font-size: 14.5px;
  font-weight: bold;
}

.side-edit { display: flex; gap: 6px; align-items: center; }
.side-edit-toggle { flex: 1; height: 34px; }

.side-help {
  color: var(--text-2);
  font-size: 12.5px;
  height: 30px;
}

/* 呼吸清單（QTableWidget 樣式：表頭、交錯列、選中列） */
.breath-table {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface);
}
.bt-header {
  display: grid;
  grid-template-columns: 44px 92px 1fr;
  gap: 6px;
  padding: 6px 8px;
  font-size: 12px;
  font-weight: bold;
  color: var(--table-header-text);
  background: var(--chip-neutral-bg);
  border-bottom: 1px solid var(--border);
}
.bt-body {
  flex: 1;
  overflow-y: auto;
  position: relative;
}
.bt-spacer { position: absolute; top: 0; left: 0; width: 1px; }
.bt-rows { position: absolute; top: 0; left: 0; right: 0; }
.bt-row {
  position: absolute;
  left: 0;
  right: 0;
  height: 30px;
  display: grid;
  grid-template-columns: 44px 92px 1fr;
  gap: 6px;
  align-items: center;
  padding: 0 8px;
  font-size: 12.5px;
  cursor: pointer;
  border-bottom: 1px solid var(--table-gridline);
}
.bt-row.bt-even { background: var(--table-alt-row); }
.bt-row:hover { background: var(--accent-subtle); }
.bt-row.selected { background: var(--accent-selection); }
.bt-col-num { color: var(--text-2); }
.bt-col-cat { display: flex; align-items: center; gap: 5px; overflow: hidden; white-space: nowrap; }
.bt-col-range {
  color: var(--text-2);
  font-family: Consolas, monospace;
  font-size: 11px;
  white-space: nowrap;
  overflow: hidden;
}

/* -------------------------------------------------------- 載入 overlay --- */

.lv-loading {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 90;
}
.lv-loading-box {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 28px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  font-size: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .18);
}
.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------- Modal --- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(31, 41, 55, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
[data-theme="eye_care"] .modal-overlay { background: rgba(0, 0, 0, .55); }
.modal {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 20px 24px;
  min-width: 340px;
  max-width: 520px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
.modal-title { font-size: 16px; font-weight: bold; margin-bottom: 10px; }
.modal-message { color: var(--text-2); white-space: pre-wrap; line-height: 1.55; }
.modal-input, .modal-select {
  width: 100%;
  margin-top: 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
}
.modal-input:focus, .modal-select:focus { outline: none; border-color: var(--accent); }
.modal-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}
.modal .btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.modal .btn-accent:hover { background: var(--accent-hover); }

/* ------------------------------------------------------ 開啟檔案對話框 --- */

.modal-open-file {
  width: min(680px, 92vw);
  max-width: none;
  display: flex;
  flex-direction: column;
  max-height: 80vh;
}
.od-body { overflow-y: auto; min-height: 180px; }
.od-empty {
  color: var(--text-2);
  text-align: center;
  padding: 32px 12px;
  line-height: 1.7;
}
.od-hint { font-size: 12px; }
.od-pick-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 14px;
  flex-wrap: wrap;
}
.od-dirname {
  font-size: 12.5px;
  color: var(--text-2);
  padding: 2px 2px 8px;
}
.od-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: border-color .12s, background .12s;
}
.od-row:hover { border-color: var(--accent); background: var(--accent-subtle); }
.od-row-main {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}
.od-name { font-weight: 600; word-break: break-all; }
.od-meta { color: var(--text-2); font-size: 12px; white-space: nowrap; }
.od-cached { color: #4CAF50; }
.od-progress { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 5px; }
.od-annotator {
  background: var(--chip-neutral-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 12px;
  color: var(--text-2);
}
.od-annotator.od-done { color: #2E7D32; border-color: #A5D6A7; }
[data-theme="eye_care"] .od-annotator.od-done { color: #81C784; border-color: #2E7D32; }
.od-annotator.od-none { color: var(--disabled-text); }
.od-foot {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 14px;
  font-size: 12.5px;
}
.od-cache { color: var(--text-2); }
.od-links { display: flex; gap: 14px; margin-left: auto; }
.od-links a { color: var(--accent); text-decoration: none; }
.od-links a:hover { text-decoration: underline; }
.od-foot .btn { margin-left: 4px; }

/* ------------------------------------------------------------ 分類選單 --- */

.menu-overlay { position: fixed; inset: 0; z-index: 110; }
.context-menu {
  position: fixed;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 4px;
  min-width: 190px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .2);
}
.menu-item {
  display: grid;
  grid-template-columns: 14px 1fr 18px 22px;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 13px;
}
.menu-item:hover { background: var(--accent-selection); }
.menu-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.menu-check { color: var(--accent); font-size: 12px; }
.menu-key { color: var(--text-2); font-size: 11px; text-align: right; }

/* -------------------------------------------------------------- Toast --- */

.toast-root {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 120;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 10px 16px;
  max-width: 380px;
  font-size: 13px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .18);
  animation: toast-in .25s ease-out;
}
.toast-ok { border-left-color: #4CAF50; }
.toast-warn { border-left-color: #FFB300; }
.toast-error { border-left-color: #F44336; }
.toast-out { opacity: 0; transition: opacity .3s; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

/* ------------------------------------------------------- 快捷鍵說明 --- */

.help-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
}
kbd {
  background: var(--chip-neutral-bg);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 7px;
  font-family: Consolas, monospace;
  font-size: 12px;
  min-width: 14px;
  text-align: center;
}

.fatal { padding: 48px; font-size: 18px; color: #D32F2F; }
