:root {
  color-scheme: light dark;
  --bg: #fbfbfa;
  --panel: #f4f4f2;
  --text: #1d1d1b;
  --muted: #8a8a85;
  --line: #e4e4e0;
  --selected: #e8e8e4;
  --danger: #b3261e;
  --ok: #3f8f5b;
  --warn: #c98a1b;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161616;
    --panel: #1c1c1c;
    --text: #e8e8e6;
    --muted: #7d7d78;
    --line: #2a2a2a;
    --selected: #2a2a29;
    --danger: #f2b8b5;
    --ok: #6fbf8a;
    --warn: #e0b35a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  outline: none;
}
input:focus { border-color: var(--muted); }

.link {
  border: 0;
  background: none;
  padding: 6px 4px;
  color: var(--muted);
}
.link:hover { color: var(--text); }
#delete:hover { color: var(--danger); }

.icon {
  flex: none;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
}
.icon svg { width: 18px; height: 18px; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; fill: none; }
.icon:hover { border-color: var(--muted); }

/* ---------- login ---------- */
#login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-box { width: min(320px, 100%); display: grid; gap: 12px; text-align: center; }
#login h1 { font-size: 22px; font-weight: 600; margin: 0; }
#login-hint:empty { display: none; }
#login-hint { margin: 0; }
#login-error { min-height: 1.5em; margin: 0; color: var(--danger); font-size: 14px; }
.muted { color: var(--muted); font-size: 14px; }

button.primary, #settings button:not(.link) {
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
}
button.primary { border: 0; background: var(--text); color: var(--bg); }
button:disabled { opacity: .5; cursor: default; }
.danger { color: var(--danger) !important; }

/* ---------- settings ---------- */
#settings {
  width: min(460px, calc(100% - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 10px 40px rgb(0 0 0 / .18);
}
#settings::backdrop { background: rgb(0 0 0 / .25); }
#settings > header, #settings > footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
}
#settings > header { border-bottom: 1px solid var(--line); }
#settings > footer { border-top: 1px solid var(--line); }
#settings h2 { margin: 0; font-size: 17px; font-weight: 600; }
#settings section { padding: 14px 16px; display: grid; gap: 8px; }
#settings section + section { border-top: 1px solid var(--line); }
#settings h3 { margin: 0; font-size: 14px; font-weight: 600; }
#settings p { margin: 0; }
#settings p:empty { display: none; }
#device-form { display: flex; gap: 8px; }
#device-code { text-transform: uppercase; letter-spacing: .08em; font-variant-numeric: tabular-nums; }
.actions { display: flex; gap: 12px; align-items: center; margin-top: 8px; }
.rows { list-style: none; margin: 0; padding: 0; }
.rows li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  font-size: 14px;
}
.rows li + li { border-top: 1px solid var(--line); }
.rows .grow { flex: 1; min-width: 0; }
.rows .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rows .sub { color: var(--muted); font-size: 12px; }

/* ---------- layout ---------- */
#app {
  display: grid;
  grid-template-columns: minmax(240px, 300px) 1fr;
  height: 100dvh;
}
#sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
#sidebar header {
  display: flex;
  gap: 8px;
  padding: max(12px, env(safe-area-inset-top)) 12px 8px max(12px, env(safe-area-inset-left));
}
#sidebar footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px max(8px, env(safe-area-inset-bottom)) 16px;
  font-size: 13px;
  color: var(--muted);
  border-top: 1px solid var(--line);
}
#status { flex: 1; display: flex; align-items: center; gap: 6px; }
#status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ok);
}
#status.pending::before { background: var(--warn); }
#status.offline::before { background: var(--muted); }

#list {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 4px 8px;
}
#list li {
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  user-select: none;
}
#list li:hover { background: color-mix(in srgb, var(--selected) 60%, transparent); }
#list li.active { background: var(--selected); }
#list .title, #list .meta {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
#list .title { font-weight: 600; font-size: 15px; }
#list .meta { color: var(--muted); font-size: 13px; }
#list .none { color: var(--muted); cursor: default; font-size: 14px; }
#list .none:hover { background: none; }

#pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}
#bar {
  display: flex;
  align-items: center;
  padding: max(8px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 0 12px;
  font-size: 14px;
}
.spacer { flex: 1; }
#editor {
  flex: 1;
  width: 100%;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  font-size: 17px;
  line-height: 1.65;
  padding: 12px max(24px, calc((100% - 70ch) / 2)) max(24px, env(safe-area-inset-bottom));
}
#empty {
  margin: auto;
  color: var(--muted);
  font-size: 14px;
}

/* ---------- mobile: one pane at a time ---------- */
@media (max-width: 720px) {
  #app { grid-template-columns: 1fr; }
  #sidebar { border-right: 0; background: var(--bg); }
  #pane { display: none; }
  body.editing #sidebar { display: none; }
  body.editing #pane { display: flex; }
  #editor { padding: 8px 20px max(20px, env(safe-area-inset-bottom)); }
  #list li { padding: 12px 10px; border-bottom: 1px solid var(--line); border-radius: 0; }
  #list li.active { background: none; }
}
@media (min-width: 721px) {
  #back { visibility: hidden; }
}
