:root {
  --primary: #1565c0;
  --primary-dark: #0d47a1;
  --accent: #ff7043;
  --bg: #f0f2f5;
  --card: #ffffff;
  --text: #1c2530;
  --muted: #6b7785;
  --border: #e2e6eb;
  --danger: #e53935;
  --ok: #2e7d32;
  --shadow: 0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
  --bar-h: 56px;
  --side-w: 300px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151a; --card: #1b212a; --text: #e7ecf2; --muted: #9aa6b2;
    --border: #2a323d; --shadow: 0 1px 3px rgba(0,0,0,.5);
  }
}
* { box-sizing: border-box; }
/* The `hidden` attribute must always win, even over class rules that set
   display (.modal-host, .field-grid). Without this, an invisible modal
   overlay stays on top and swallows every click. */
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Segoe UI", "Malgun Gothic", system-ui, sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

/* ===== App bar ===== */
.appbar {
  height: var(--bar-h); display: flex; align-items: center; gap: 8px;
  padding: 0 10px; background: var(--primary); color: #fff;
  position: sticky; top: 0; z-index: 30; box-shadow: var(--shadow);
}
.appbar-logo { width: 30px; height: 30px; border-radius: 6px; }
.appbar-title { display: flex; flex-direction: column; line-height: 1.1; flex: 1; min-width: 0; }
.appbar-title span { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar-title small { font-size: 11px; opacity: .85; }
.appbar-actions { display: flex; align-items: center; gap: 2px; }
.icon-btn {
  background: transparent; border: 0; color: inherit; cursor: pointer;
  width: 38px; height: 38px; border-radius: 50%; font-size: 18px;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: rgba(255,255,255,.15); }
.auto-badge { font-size: 11px; padding: 2px 7px; border-radius: 10px; background: rgba(255,255,255,.2); }
.auto-badge.on { background: #43a047; }

/* ===== Layout ===== */
.layout { display: flex; height: calc(100% - var(--bar-h)); }
.sidebar {
  width: var(--side-w); flex: 0 0 var(--side-w); background: var(--card);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
  z-index: 25;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); font-weight: 600; }
.channel-list { flex: 1; overflow-y: auto; padding: 8px; }
.sidebar-foot { display: flex; gap: 6px; padding: 8px 10px; border-top: 1px solid var(--border); }
.hint { font-size: 11px; color: var(--muted); padding: 0 14px 12px; margin: 0; }

.channel-item {
  display: flex; align-items: center; gap: 10px; padding: 11px 12px; border-radius: 10px;
  cursor: pointer; margin-bottom: 4px; border: 1px solid transparent; user-select: none;
}
.channel-item:hover { background: var(--bg); }
.channel-item.active { background: color-mix(in srgb, var(--primary) 14%, transparent); border-color: color-mix(in srgb, var(--primary) 40%, transparent); }
.channel-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--ok); flex: 0 0 auto; }
.channel-dot.stale { background: var(--muted); }
.channel-dot.alarm { background: var(--danger); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: .25; } }
.channel-meta { flex: 1; min-width: 0; }
.channel-meta b { display: block; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.channel-meta small { font-size: 11px; color: var(--muted); }

.scrim { display: none; }

/* ===== Content ===== */
.content { flex: 1; overflow-y: auto; padding: 14px; }
.empty-state { max-width: 420px; margin: 8vh auto; text-align: center; color: var(--muted); }
.empty-state img { width: 72px; height: 72px; opacity: .85; }
.empty-state h2 { margin: 14px 0 6px; color: var(--text); }

.field-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.field-card {
  background: var(--card); border-radius: 14px; padding: 14px; box-shadow: var(--shadow);
  border-top: 4px solid var(--primary); cursor: pointer; position: relative;
  transition: transform .08s; user-select: none;
}
.field-card:active { transform: scale(.985); }
.field-card.alarm { animation: cardAlarm 1.1s ease-in-out infinite; }
@keyframes cardAlarm { 0%,100% { box-shadow: var(--shadow); } 50% { box-shadow: 0 0 0 3px var(--danger); } }
.field-card .fc-name { font-size: 13px; color: var(--muted); display: flex; justify-content: space-between; align-items: center; }
.field-card .fc-value { font-size: 34px; font-weight: 700; margin: 6px 0 2px; line-height: 1.1; word-break: break-all; }
.field-card .fc-unit { font-size: 14px; color: var(--muted); font-weight: 400; }
.field-card .fc-time { font-size: 11px; color: var(--muted); }
.field-card .fc-gear { opacity: .5; font-size: 15px; }
.field-card .fc-gear:hover { opacity: 1; }
.field-card .fc-alarm-tag { position: absolute; top: 10px; right: 36px; font-size: 11px; color: var(--danger); }

/* ===== Buttons ===== */
.primary-btn, .text-btn, .ghost-btn {
  font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid transparent; padding: 9px 14px;
}
.primary-btn { background: var(--primary); color: #fff; font-weight: 600; }
.primary-btn:hover { background: var(--primary-dark); }
.text-btn { background: transparent; color: var(--primary); border-color: transparent; padding: 6px 8px; font-weight: 600; }
.text-btn.small { font-size: 12px; flex: 1; border: 1px solid var(--border); }
.text-btn:hover { background: var(--bg); }
.ghost-btn { background: var(--bg); color: var(--text); border-color: var(--border); }
.danger-btn { background: var(--danger); color: #fff; border: 0; padding: 9px 14px; border-radius: 8px; cursor: pointer; font: inherit; }

/* ===== Modal ===== */
.modal-host { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.45); }
.modal {
  position: relative; background: var(--card); color: var(--text); width: min(560px, 94vw);
  max-height: 90vh; overflow-y: auto; border-radius: 16px; box-shadow: 0 12px 40px rgba(0,0,0,.3);
  padding: 0;
}
.modal-header { padding: 16px 20px; font-size: 18px; font-weight: 700; border-bottom: 1px solid var(--border); }
.modal-content { padding: 16px 20px; }
.modal-actions { padding: 12px 20px; display: flex; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.modal-actions .spacer { flex: 1; }

.form-row { margin-bottom: 14px; }
.form-row label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
.form-row input[type=text], .form-row input[type=number], .form-row input[type=password], .form-row select {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); font: inherit;
}
.form-row.inline { display: flex; gap: 10px; }
.form-row.inline > div { flex: 1; }
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.color-swatch { width: 26px; height: 26px; border-radius: 6px; border: 1px solid var(--border); cursor: pointer; padding: 0; }

/* Field settings list (within a channel) */
.fs-list { display: flex; flex-direction: column; gap: 6px; }
.fs-row { display: flex; align-items: center; gap: 10px; padding: 8px; border: 1px solid var(--border); border-radius: 8px; }
.fs-row .fs-color { width: 22px; height: 22px; border-radius: 5px; flex: 0 0 auto; }
.fs-row .fs-name { flex: 1; }
.fs-row input[type=checkbox] { width: 18px; height: 18px; }

/* Chart modal */
.chart-wrap { height: 340px; position: relative; }
.period-bar { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.period-bar .chip {
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  border-radius: 16px; padding: 5px 12px; cursor: pointer; font-size: 13px;
}
.period-bar .chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* Color palette */
.palette { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.palette button { width: 100%; aspect-ratio: 1; border-radius: 6px; border: 2px solid transparent; cursor: pointer; }
.palette button.sel { border-color: var(--text); }

/* Toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #323232; color: #fff; padding: 11px 18px; border-radius: 24px;
  font-size: 14px; z-index: 100; box-shadow: var(--shadow); max-width: 88vw;
}

/* Long-press context menu */
.ctx-menu { position: fixed; background: var(--card); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.25); z-index: 60; overflow: hidden; min-width: 180px; }
.ctx-menu button { display: block; width: 100%; text-align: left; padding: 11px 16px; background: transparent; border: 0; color: var(--text); font: inherit; cursor: pointer; }
.ctx-menu button:hover { background: var(--bg); }
.ctx-menu button.danger { color: var(--danger); }

/* ===== Mobile ===== */
@media (max-width: 760px) {
  /* Off-canvas drawer driven by `left` (transform proved unreliable in some webviews) */
  .sidebar {
    position: fixed; top: var(--bar-h); bottom: 0; left: -88%;
    width: 86%; max-width: 320px;
    transition: left .24s ease; box-shadow: 4px 0 22px rgba(0,0,0,.35); z-index: 40;
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.drawer-open .sidebar { left: 0; }
  body.drawer-open .scrim {
    display: block; position: fixed; inset: var(--bar-h) 0 0 0;
    background: rgba(0,0,0,.45); z-index: 35;
  }

  /* Compact app bar: declutter to menu · title · refresh · overflow */
  .appbar { padding: 0 6px; gap: 4px; padding-top: env(safe-area-inset-top); height: calc(var(--bar-h) + env(safe-area-inset-top)); }
  .appbar-logo { display: none; }
  #btnExport, #autoBadge { display: none; }
  .appbar-title span { font-size: 17px; }

  /* Two-column dashboard of field cards */
  .content { padding: 10px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
  .field-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .field-card { padding: 12px; border-radius: 12px; }
  .field-card .fc-value { font-size: 26px; margin: 4px 0 2px; }
  .field-card .fc-name { font-size: 12px; }
  .field-card .fc-time { font-size: 10px; }

  /* Dialogs behave like bottom-anchored sheets */
  .modal-host { align-items: flex-end; }
  .modal { width: 100vw; max-height: 92vh; border-radius: 18px 18px 0 0; }
  .modal-actions { position: sticky; bottom: 0; background: var(--card); padding-bottom: calc(12px + env(safe-area-inset-bottom)); }

  .empty-state { margin: 12vh auto; padding: 0 16px; }
  .chart-wrap { height: 46vh; }
}

/* Very narrow phones: single column */
@media (max-width: 340px) {
  .field-grid { grid-template-columns: 1fr; }
}
