/* ============================================================
   QScada 工業監控平台 — 設計系統
   風格沿用 KINGNEWTON EMS（淺色商務風 / Ant 藍）
   ============================================================ */
:root {
  --font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Microsoft JhengHei", "Helvetica Neue", Arial, sans-serif;

  /* Color Palette */
  --bg-main: #f4f6fa;
  --bg-sidebar: #0b0f19;
  --bg-sidebar-hover: #1e293b;
  --bg-sidebar-active: #1890ff;
  --bg-header: #0c1424;
  --border-color: #e2e8f0;

  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --text-light: #94a3b8;

  /* Status Colors */
  --primary-blue: #1890ff;
  --success-green: #52c41a;
  --warning-orange: #fa8c16;
  --danger-red: #f5222d;
  --purple-accent: #722ed1;
  --yellow-accent: #fadb14;
  --grey-inactive: #8c8c8c;

  /* Box Shadows */
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.05), 0 4px 6px -2px rgba(0, 0, 0, 0.02);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font-family);
  background-color: var(--bg-main);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* ── Header ───────────────────────────────────────────── */
.header-container {
  height: 60px;
  background-color: var(--bg-header);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  border-bottom: 1px solid #1e293b;
  z-index: 10;
  flex-shrink: 0;
}
.brand-section { display: flex; align-items: center; gap: 12px; }
.brand-logo-box {
  width: 34px; height: 34px; border-radius: 8px;
  background: linear-gradient(135deg, #1890ff, #2f54eb);
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 12px rgba(24, 144, 255, 0.5);
}
.brand-title { font-size: 16px; font-weight: 600; letter-spacing: 0.5px; }
.header-controls { display: flex; align-items: center; gap: 24px; }
.clock-display { font-size: 14px; color: #94a3b8; font-family: monospace; }
.header-status-pill {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: #94a3b8;
  padding: 4px 10px; border-radius: 16px; background: rgba(255,255,255,0.05);
}
.hdr-dot { width: 8px; height: 8px; border-radius: 50%; }
.hdr-dot.on { background: var(--success-green); box-shadow: 0 0 8px var(--success-green); }
.hdr-dot.off { background: var(--danger-red); }
.notification-badge { position: relative; cursor: pointer; display: flex; align-items: center; }
.notification-count {
  position: absolute; top: -6px; right: -8px;
  background-color: var(--danger-red); color: #fff;
  font-size: 10px; font-weight: bold; padding: 2px 5px;
  border-radius: 10px; line-height: 1;
}
/* 登入者資訊 + 登出（header 右側）*/
.header-user {
  display: flex; align-items: center; gap: 8px;
  padding-left: 16px; border-left: 1px solid rgba(255,255,255,0.12);
}
.header-user-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: rgba(24,144,255,0.18); color: #69c0ff;
  display: flex; align-items: center; justify-content: center;
}
.header-user-name { font-size: 13px; color: #e2e8f0; font-weight: 500; }
.header-logout {
  display: inline-flex; align-items: center; gap: 6px;
  height: 32px; padding: 0 12px;
  background: rgba(255,255,255,0.06); color: #cbd5e1;
  border: 1px solid rgba(255,255,255,0.12); border-radius: 8px;
  font-size: 13px; font-weight: 500; cursor: pointer;
  transition: all 0.15s ease;
}
.header-logout:hover {
  background: rgba(245,34,45,0.15); color: #ff7875;
  border-color: rgba(245,34,45,0.4);
}

/* ── Main wrapper / sidebar ───────────────────────────── */
.main-wrapper { flex: 1; display: flex; overflow: hidden; position: relative; }

.sidebar {
  width: 208px; background-color: var(--bg-sidebar);
  display: flex; flex-direction: column; align-items: stretch;
  padding: 14px 12px; gap: 2px; z-index: 5; flex-shrink: 0;
  overflow-y: auto;
}
/* 分組標題 */
.sidebar-group {
  font-size: 11px; font-weight: 600; letter-spacing: 0.5px;
  color: #475569; text-transform: none;
  padding: 14px 12px 6px; user-select: none;
}
.sidebar-group:first-child { padding-top: 4px; }
/* 導覽項：icon + 文字 */
.sidebar-item {
  display: flex; align-items: center; gap: 12px;
  height: 40px; padding: 0 12px;
  border-radius: 8px; color: #94a3b8; cursor: pointer;
  position: relative; transition: all 0.18s ease; text-decoration: none;
  font-size: 14px; white-space: nowrap;
}
.sidebar-item:hover { background-color: var(--bg-sidebar-hover); color: #fff; }
.sidebar-item.active {
  background-color: var(--bg-sidebar-active); color: #fff; font-weight: 500;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.35);
}
.sidebar-icon { display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.sidebar-item svg { width: 18px; height: 18px; }
.sidebar-label { flex: 1; overflow: hidden; text-overflow: ellipsis; }

/* ── Content viewport ─────────────────────────────────── */
.content-viewport {
  flex: 1; overflow-y: auto; padding: 20px;
  display: flex; flex-direction: column; gap: 20px;
}
.page-title-row { display: flex; align-items: center; justify-content: space-between; }
.page-title { font-size: 20px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.page-title-sub { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

/* ── KPI Cards ────────────────────────────────────────── */
.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.kpi-card {
  background-color: #fff; border-radius: 12px; padding: 18px;
  display: flex; align-items: center; gap: 16px;
  border: 1px solid var(--border-color); box-shadow: var(--shadow-sm);
  transition: all 0.2s ease;
}
.kpi-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.kpi-icon-container {
  width: 48px; height: 48px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.kpi-card:nth-child(1) .kpi-icon-container { background-color: rgba(24, 144, 255, 0.1); color: var(--primary-blue); }
.kpi-card:nth-child(2) .kpi-icon-container { background-color: rgba(82, 196, 26, 0.1); color: var(--success-green); }
.kpi-card:nth-child(3) .kpi-icon-container { background-color: rgba(250, 140, 22, 0.1); color: var(--warning-orange); }
.kpi-card:nth-child(4) .kpi-icon-container { background-color: rgba(114, 46, 209, 0.1); color: var(--purple-accent); }
.kpi-info { flex: 1; min-width: 0; }
.kpi-title { font-size: 13px; color: var(--text-secondary); margin-bottom: 6px; }
.kpi-value-row { display: flex; align-items: baseline; gap: 6px; }
.kpi-value { font-size: 24px; font-weight: 700; font-family: monospace; }
.kpi-unit { font-size: 12px; color: var(--text-secondary); }
.kpi-trend { font-size: 11px; display: flex; align-items: center; gap: 4px; margin-top: 4px; color: var(--text-light); }

/* ── Cards / panels ───────────────────────────────────── */
.dashboard-card, .table-card, .filter-card {
  background-color: #fff; border-radius: 12px;
  border: 1px solid var(--border-color); box-shadow: var(--shadow-sm);
}
.dashboard-card { display: flex; flex-direction: column; padding: 20px; position: relative; }
.card-header-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.card-header-title { font-size: 15px; font-weight: 600; color: var(--text-primary); display: flex; align-items: center; gap: 8px; }
.card-header-link { font-size: 12px; color: var(--primary-blue); cursor: pointer; }
.card-header-link:hover { text-decoration: underline; }

/* Dashboard grid split */
.content-split { display: grid; grid-template-columns: 1.6fr 1fr; gap: 20px; align-items: stretch; }

/* ── Charts ───────────────────────────────────────────── */
.chart-wrapper { flex: 1; min-height: 200px; width: 100%; }
.chart-wrapper-lg { min-height: 380px; width: 100%; }

/* ── Filter card ──────────────────────────────────────── */
.filter-card {
  padding: 20px; display: grid; grid-template-columns: repeat(6, 1fr);
  gap: 16px; align-items: end;
}
.filter-grid-item { display: flex; flex-direction: column; gap: 6px; }
.filter-grid-item label { font-size: 12px; color: var(--text-secondary); font-weight: 500; }

.form-input, .form-select {
  height: 36px; padding: 0 12px; border-radius: 6px;
  border: 1px solid var(--border-color); outline: none;
  font-size: 13px; width: 100%; color: var(--text-primary);
  background-color: #fff; transition: border-color 0.15s ease;
  font-family: inherit;
}
.form-select { cursor: pointer; }
.form-input:focus, .form-select:focus { border-color: var(--primary-blue); }
textarea.form-input { height: auto; min-height: 64px; padding: 8px 12px; font-family: monospace; resize: vertical; }

/* ── Buttons ──────────────────────────────────────────── */
.btn-primary, .btn-secondary {
  height: 36px; border-radius: 6px; font-size: 13px; font-weight: 500;
  cursor: pointer; display: inline-flex; align-items: center;
  justify-content: center; gap: 6px; padding: 0 16px; transition: all 0.15s ease;
  font-family: inherit;
}
.btn-primary { background-color: var(--primary-blue); color: #fff; border: none; }
.btn-primary:hover { background-color: #40a9ff; }
.btn-primary:disabled { background-color: #bae0ff; cursor: not-allowed; }
.btn-secondary { background-color: #fff; color: var(--text-secondary); border: 1px solid var(--border-color); }
.btn-secondary:hover { background-color: #f1f5f9; border-color: #cbd5e1; }
.btn-block { width: 100%; }

/* ── Data tables ──────────────────────────────────────── */
.table-card { padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.data-table-container { overflow-x: auto; width: 100%; }
.data-table { width: 100%; border-collapse: collapse; text-align: left; font-size: 13px; }
.data-table th {
  background-color: #f8fafc; padding: 12px 16px; font-weight: 600;
  color: var(--text-secondary); border-bottom: 2px solid var(--border-color); white-space: nowrap;
}
.data-table td { padding: 12px 16px; border-bottom: 1px solid var(--border-color); color: var(--text-primary); vertical-align: middle; }
.data-table tbody tr:hover { background-color: #f8fafc; }
.mono { font-family: monospace; color: var(--text-secondary); }

/* status pills */
.status-pill { padding: 4px 8px; border-radius: 12px; font-size: 11px; font-weight: 500; display: inline-block; text-align: center; }
.status-pill.online { background-color: rgba(82, 196, 26, 0.12); color: var(--success-green); }
.status-pill.offline { background-color: #f1f5f9; color: var(--text-secondary); }
.status-pill.standby { background-color: rgba(24, 144, 255, 0.12); color: var(--primary-blue); }
.status-pill.abnormal { background-color: rgba(245, 34, 45, 0.12); color: var(--danger-red); }

/* level pills (告警等級) */
.level-pill { padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: bold; display: inline-block; }
.level-pill.lv0 { background-color: #f1f5f9; color: var(--text-secondary); }
.level-pill.lv1 { background-color: rgba(250, 219, 20, 0.15); color: #d4b106; }
.level-pill.lv2 { background-color: rgba(250, 140, 22, 0.15); color: var(--warning-orange); }
.level-pill.lv3 { background-color: rgba(245, 34, 45, 0.15); color: var(--danger-red); }
.level-pill.lv4 { background-color: var(--danger-red); color: #fff; }

/* action links */
.btn-action-link { color: var(--primary-blue); background: none; border: none; cursor: pointer; font-size: 12px; font-weight: 500; margin-right: 12px; font-family: inherit; }
.btn-action-link:hover { text-decoration: underline; }
.btn-action-link.red { color: var(--danger-red); }

/* ── Empty / loading state ────────────────────────────── */
.empty-state {
  padding: 48px 0; text-align: center; color: var(--text-light); font-size: 13px;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.empty-state svg { opacity: 0.4; }
.loading-state { padding: 40px 0; text-align: center; color: var(--text-light); font-size: 13px; }

/* ── Modal ────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; background-color: rgba(0,0,0,0.5);
  display: none; align-items: center; justify-content: center; z-index: 100;
}
.modal-overlay.active { display: flex; }
.modal-card {
  background-color: #fff; border-radius: 12px; width: 540px; max-width: 92%;
  box-shadow: var(--shadow-lg); overflow: hidden;
  animation: modalSlide 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes modalSlide { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-header { padding: 16px 20px; border-bottom: 1px solid var(--border-color); display: flex; align-items: center; justify-content: space-between; }
.modal-title { font-weight: 600; font-size: 15px; }
.modal-close { cursor: pointer; color: var(--text-light); display: flex; }
.modal-close:hover { color: var(--text-primary); }
.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 14px; max-height: 70vh; overflow-y: auto; }
.modal-footer { padding: 14px 20px; border-top: 1px solid var(--border-color); display: flex; justify-content: flex-end; gap: 10px; }
.form-row2 { display: flex; gap: 14px; }
.form-row2 > * { flex: 1; }
.form-field { display: flex; flex-direction: column; gap: 6px; }
.form-field label { font-size: 12px; color: var(--text-secondary); font-weight: 500; }
.form-check { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.form-check input { width: auto; }
.form-err { color: var(--danger-red); font-size: 12px; min-height: 16px; }

/* ── Toast ────────────────────────────────────────────── */
.toast-container { position: fixed; top: 75px; right: 20px; display: flex; flex-direction: column; gap: 12px; z-index: 9999; max-width: 360px; }
.toast {
  background: rgba(15, 23, 42, 0.96); backdrop-filter: blur(12px);
  border: 1px solid rgba(255,255,255,0.15); border-left: 4px solid var(--primary-blue);
  border-radius: 10px; padding: 14px 16px; color: #f1f5f9; font-size: 13px;
  box-shadow: 0 10px 25px -5px rgba(0,0,0,0.3);
  transform: translateX(120%); opacity: 0; transition: all 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}
.toast.show { transform: translateX(0); opacity: 1; }
.toast.ok { border-left-color: var(--success-green); }
.toast.bad { border-left-color: var(--danger-red); }

/* ── Settings info list ───────────────────────────────── */
.info-list { display: flex; flex-direction: column; }
.info-row { display: flex; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid #f1f5f9; font-size: 13px; }
.info-row:last-child { border-bottom: none; }
.info-row .k { color: var(--text-secondary); }
.info-row .v { font-weight: 500; font-family: monospace; }

/* segmented control (時間單位等) */
.segmented { display: flex; border: 1px solid var(--border-color); border-radius: 6px; overflow: hidden; height: 36px; }
.segmented button { flex: 1; border: none; background: #fff; color: var(--text-secondary); font-size: 13px; cursor: pointer; font-family: inherit; }
.segmented button.active { background: var(--primary-blue); color: #fff; }

/* live reading grid */
.reading-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.reading-cell { background: #f8fafc; border: 1px solid var(--border-color); border-radius: 10px; padding: 14px; }
.reading-cell .rc-key { font-size: 12px; color: var(--text-secondary); margin-bottom: 6px; display: flex; justify-content: space-between; align-items: center; }
.reading-cell .rc-val { font-size: 22px; font-weight: 700; font-family: monospace; color: var(--text-primary); }
.reading-cell .rc-dev { font-size: 11px; color: var(--text-light); font-family: monospace; margin-top: 4px; }
.pulse-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--success-green); box-shadow: 0 0 0 rgba(82,196,26,0.6); animation: pulse 2s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(82,196,26,0.5); } 70% { box-shadow: 0 0 0 6px rgba(82,196,26,0); } 100% { box-shadow: 0 0 0 0 rgba(82,196,26,0); } }

/* live message log */
.msg-log { max-height: 320px; overflow: auto; }
.msg-log td.topic { color: #0d9488; font-family: monospace; }
.msg-log td.payload { color: var(--text-secondary); font-family: monospace; max-width: 480px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Responsive ───────────────────────────────────────── */
@media (max-width: 1100px) {
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
  .content-split { grid-template-columns: 1fr; }
  .filter-card { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 640px) {
  .kpi-row { grid-template-columns: 1fr; }
  .filter-card { grid-template-columns: 1fr; }
  .clock-display, .header-status-pill { display: none; }
  .sidebar { width: 168px; }
}
