:root {
  --bg: #eef1f4;
  --panel-bg: #ffffff;
  --line: #e6e8eb;
  --line-strong: #d6dadf;
  --text: #1c1f23;
  --muted: #8a929c;
  --accent: #2f6bff;
  --accent-soft: #eaf0ff;
  --danger: #e0394f;
  --ok: #1fc16b;
  --warn: #f5a524;
  --key-bg: #f6f7f9;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 30px rgba(15, 23, 42, 0.10);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: contain;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

button, input, textarea, select { font: inherit; color: inherit; }
button { -webkit-appearance: none; appearance: none; }

/* ============ LOGIN ============ */
#login {
  display: none;
  position: fixed; inset: 0;
  background: var(--bg);
  z-index: 30;
  place-items: center;
  padding: 20px;
}
.login-card {
  width: 100%; max-width: 360px;
  background: #fff; border-radius: 20px;
  padding: 28px 24px;
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; gap: 12px;
}
.login-logo {
  font-weight: 800; font-size: 28px; letter-spacing: -0.6px;
  text-align: center; color: var(--accent); margin-bottom: 6px;
}
.login-card input {
  padding: 14px 16px;
  border: 1px solid var(--line-strong); border-radius: 12px;
  background: #fff; font-size: 16px;
}
.login-card input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.login-card button {
  margin-top: 4px; padding: 14px;
  border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; font-weight: 600; font-size: 16px;
  cursor: pointer;
}

/* ============ TOPBAR ============ */
#topbar {
  background: #fff;
  border-bottom: 1px solid var(--line);
  padding: 12px 18px;
  display: flex; align-items: center; justify-content: space-between;
  position: sticky; top: 0; z-index: 10;
}
.logo { font-weight: 800; font-size: 22px; letter-spacing: -0.5px; color: var(--accent); }
.topbar-action {
  background: transparent; border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 12px;
  font-size: 13px; color: var(--muted); cursor: pointer;
}
.topbar-action:active { background: var(--bg); }

/* ============ DEVICE LIST ============ */
#devices-view { padding: 18px; }
.dv-title { margin: 0 0 4px; font-size: 24px; font-weight: 800; }
.dv-sub { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
.dv-empty { color: var(--muted); }
#device-list {
  display: grid; gap: 14px;
  grid-template-columns: 1fr;
}

.device-card {
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
}
.dc-top { display: flex; align-items: center; gap: 10px; }
.dc-name {
  flex: 1; font-size: 17px; font-weight: 700; border: 0;
  border-bottom: 1px dashed var(--line);
  background: transparent; padding: 4px 0; color: var(--text);
  min-width: 0;
}
.dc-name:focus { outline: none; border-bottom-color: var(--accent); }
.dc-dot { width: 11px; height: 11px; border-radius: 50%; flex: none; }
.dc-dot.ok { background: var(--ok); } .dc-dot.warn { background: var(--warn); } .dc-dot.off { background: #c2c7cd; }
.dc-meta { font-size: 12px; color: var(--muted); }

/* ---- timer pill ---- */
.timer {
  background: var(--key-bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex; flex-direction: column; gap: 6px;
}
.timer.running {
  background: var(--accent-soft);
  border-color: var(--accent);
}
.timer-head { font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 6px; letter-spacing: 0.2px; }
.timer.running .timer-head { color: var(--accent); }
.timer-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.timer-time {
  font-variant-numeric: tabular-nums;
  font-size: 28px; font-weight: 800; letter-spacing: -0.5px;
  color: var(--text);
}
.timer.running .timer-time { color: var(--accent); }
.timer-btn {
  padding: 10px 16px;
  border: 0; border-radius: 999px;
  background: var(--accent); color: #fff;
  font-weight: 600; font-size: 14px;
  cursor: pointer; min-height: 40px;
}
.timer.running .timer-btn { background: var(--danger); }
.timer-label { font-size: 12px; color: var(--muted); }

/* ---- history ---- */
.hist {
  font-size: 13px;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}
.hist summary {
  cursor: pointer; color: var(--muted);
  padding: 4px 0; list-style: none;
}
.hist summary::-webkit-details-marker { display: none; }
.hist summary::before { content: "›"; display: inline-block; transform: rotate(0deg); transition: transform .15s; margin-right: 6px; }
.hist[open] summary::before { transform: rotate(90deg); }
.hist-count { color: #b6bbc2; }
.hist ul { list-style: none; margin: 6px 0 0; padding: 0; }
.hist li {
  display: flex; justify-content: space-between; gap: 8px;
  padding: 6px 0; border-top: 1px dashed var(--line);
  font-variant-numeric: tabular-nums;
}
.hist li.hist-empty { color: var(--muted); border-top: 0; }
.hist-when { color: var(--muted); }
.hist-dur { font-weight: 600; }

.dc-actions { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.dc-enter {
  padding: 12px 16px; border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; cursor: pointer;
  font-size: 15px; font-weight: 600; min-height: 44px;
  text-decoration: none; text-align: center;
  display: inline-flex; align-items: center; justify-content: center;
}
.dc-enter-secondary {
  background: #fff; color: var(--accent);
  border: 1px solid var(--accent);
  font-size: 13px; padding: 12px 14px;
}
.dc-enter[data-disabled] { background: var(--key-bg); color: var(--muted); cursor: not-allowed; border-color: var(--line); }
.dc-enter-secondary[data-disabled] { background: #fff; }

/* ============ CONTROL ============ */
#control-view {
  display: flex; flex-direction: column; gap: 12px;
  padding: 12px;
  min-height: calc(100dvh - 60px - var(--safe-top) - var(--safe-bottom));
}
.back-btn {
  align-self: flex-start;
  padding: 8px 14px; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; cursor: pointer; font-size: 14px; min-height: 36px;
}

/* phone screen */
#stage {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  min-height: 0;
}
#screen-wrap {
  position: relative;
  height: 64vh;              /* size to phone aspect so there are no side bars */
  max-height: 620px;
  max-width: 100%;
  aspect-ratio: 393 / 852;
  margin: 0 auto;
  background: #000;
  border-radius: 22px;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  touch-action: none;
}
#screen { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; background: #000; }
#overlay { position: absolute; inset: 0; touch-action: none; cursor: crosshair; }
#status { font-size: 12px; color: var(--muted); }

/* control panel */
#panel { display: flex; flex-direction: column; gap: 12px; }

.card {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
}
.timer-card { padding: 12px; }
.timer-card .timer { border: 0; }
.timer-card .timer:not(.running) { background: var(--accent-soft); border: 1px solid var(--line); }

.field label, .section-label, .col-head {
  display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px;
}
.field .value { font-size: 17px; font-weight: 600; }

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px 16px;
  align-items: start;
}
.steppers { display: flex; flex-direction: column; gap: 8px; }
.stepper { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 14px; color: #555; }
.stepper input {
  width: 70px; text-align: right; font-size: 15px;
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px;
  -webkit-appearance: none; appearance: none;
  min-height: 36px;
}
.save-btn {
  margin-top: 4px; width: 100%; padding: 10px;
  border: 1px solid var(--line-strong); border-radius: 10px;
  background: #fff; cursor: pointer; font-size: 14px; min-height: 40px;
}

.control-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.col-head { text-align: center; font-size: 13px; color: #444; margin-bottom: 8px; font-weight: 600; }

/* D-pad */
.dpad {
  display: grid;
  grid-template-columns: repeat(3, minmax(44px, 1fr));
  grid-auto-rows: 48px;
  gap: 8px;
  justify-content: center;
}
.key {
  border: 1px solid var(--line);
  background: var(--key-bg);
  border-radius: 10px;
  font-size: 18px; cursor: pointer;
  display: grid; place-items: center;
  min-height: 44px;
}
.key:active { background: #dce0e6; transform: scale(0.96); }
.dpad-warning {
  margin: 10px 0 0; text-align: center;
  font-size: 10px; font-weight: 700; line-height: 1.3;
  color: var(--muted); letter-spacing: 0.2px;
}

/* Gestures list */
.gestures { display: flex; flex-direction: column; gap: 6px; }
.gesture {
  padding: 10px; border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff; cursor: pointer; font-size: 14px; font-weight: 500;
  min-height: 40px; text-align: center;
}
.gesture.selected {
  border-color: var(--accent); color: var(--accent);
  background: var(--accent-soft);
  font-weight: 700;
}
.gesture:active { background: var(--bg); }

#message {
  width: 100%; margin-top: 12px; min-height: 64px; resize: vertical;
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font-size: 16px;
  font-family: inherit;
}
.send-btn {
  width: 100%; margin-top: 10px; padding: 12px;
  border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; cursor: pointer;
  font-size: 15px; font-weight: 600; min-height: 44px;
}

/* ============ TABLET / DESKTOP ============ */
@media (min-width: 720px) {
  #devices-view { padding: 28px; }
  #device-list { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
  body { background: var(--bg); border: 6px solid var(--accent); }
}

@media (min-width: 1024px) {
  #control-view {
    display: grid;
    grid-template-columns: minmax(380px, 460px) 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "back  back"
      "panel stage";
    gap: 18px;
    padding: 16px 22px;
    height: calc(100vh - 60px);
    box-sizing: border-box;
  }
  .back-btn { grid-area: back; }
  #panel {
    grid-area: panel;
    overflow-y: auto; max-height: 100%;
    padding-right: 4px;
  }
  #stage {
    grid-area: stage;
    min-height: 0;
    height: 100%;
    align-items: center; justify-content: flex-start;
  }
  #screen-wrap {
    width: auto; max-width: 100%;
    height: 100%;
    max-height: none;
    aspect-ratio: 393 / 852;
  }
}

/* ============ TOUCH-DEVICE NICETIES ============ */
@media (hover: none) and (pointer: coarse) {
  .key, .gesture, .timer-btn, .dc-enter, .save-btn, .send-btn { user-select: none; }
  #overlay { cursor: default; }
}

/* prevent iOS double-tap zoom on rapid taps */
button, .dc-enter, .timer-btn, .key, .gesture { touch-action: manipulation; }

/* ============ LOADING SCREEN ============ */
/* Overlays the phone view on entry; fades out once the live image is up & not
   black (or after the 7s cap). Blocks touch control while visible. */
.loader {
  position: absolute; inset: 0; z-index: 20;
  display: grid; place-items: center;
  background: radial-gradient(125% 125% at 50% 38%, rgba(30,37,66,.74), rgba(7,9,20,.94));
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  opacity: 0; transition: opacity .35s ease;
  border-radius: 22px;
}
.loader.show { opacity: 1; }
.loader-card {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  transform: translateY(8px) scale(.97);
  transition: transform .4s cubic-bezier(.2,.8,.2,1);
}
.loader.show .loader-card { transform: none; }

.loader-brand {
  font-weight: 800; font-size: 30px; letter-spacing: -0.6px;
  background: linear-gradient(120deg, #8fb0ff, #c79bff);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; color: #fff;
  animation: loaderBrand 2.2s ease-in-out infinite;
}
@keyframes loaderBrand {
  0%,100% { filter: drop-shadow(0 0 8px rgba(91,140,255,.45)); opacity: .92; }
  50%     { filter: drop-shadow(0 0 22px rgba(155,91,255,.85)); opacity: 1; }
}

.loader-ring { position: relative; width: 130px; height: 130px; }
.lr-svg { width: 100%; height: 100%; transform: rotate(-90deg); display: block; }
.lr-track { fill: none; stroke: rgba(255,255,255,.12); stroke-width: 8; }
.lr-prog {
  fill: none; stroke: url(#lrgrad); stroke-width: 8; stroke-linecap: round;
  stroke-dasharray: 327; stroke-dashoffset: 0;
  filter: drop-shadow(0 0 6px rgba(120,130,255,.65));
  animation: lrDeplete 7s linear forwards;
}
@keyframes lrDeplete { to { stroke-dashoffset: 327; } }

.loader-count {
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 46px; font-weight: 800; color: #fff;
  font-variant-numeric: tabular-nums; text-shadow: 0 2px 12px rgba(0,0,0,.45);
}
.loader-count span { display: inline-block; animation: loaderPulse 1s ease-in-out infinite; }
@keyframes loaderPulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.09); } }

.loader-text { color: #fff; font-size: 16px; font-weight: 700; letter-spacing: .2px; }
.loader-sub { color: rgba(255,255,255,.6); font-size: 12px; display: flex; align-items: center; gap: 5px; }
.loader-dots { display: inline-flex; gap: 3px; }
.loader-dots i { width: 4px; height: 4px; border-radius: 50%; background: rgba(255,255,255,.6); animation: loaderDot 1.2s ease-in-out infinite; }
.loader-dots i:nth-child(2) { animation-delay: .2s; }
.loader-dots i:nth-child(3) { animation-delay: .4s; }
@keyframes loaderDot { 0%,100% { opacity: .3; transform: translateY(0); } 50% { opacity: 1; transform: translateY(-3px); } }

@media (prefers-reduced-motion: reduce) {
  .loader-brand, .loader-count span, .loader-dots i, .lr-prog { animation: none; }
}
