/* Ctrl: mobile-first styles. Light and dark themes via CSS variables. */

:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #d1d5db;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --ok: #15803d;
  --warn: #b45309;
  --info: #1d4ed8;
  --neutral: #4b5563;
  --bar: #1f2937;
  --bar-text: #f9fafb;
  --radius: 10px;
  --tabbar-h: 56px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b;
    --surface: #1a212b;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #374151;
    --accent: #3b82f6;
    --danger: #f87171;
    --ok: #4ade80;
    --warn: #fbbf24;
    --info: #93c5fd;
    --neutral: #9ca3af;
    --bar: #111827;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}

a {
  color: var(--accent);
}

h1 {
  font-size: 1.4rem;
  margin: 0.25rem 0 0.75rem;
}

h2 {
  font-size: 1.05rem;
  margin: 0 0 0.6rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  justify-content: space-between;
}

h3 {
  font-size: 0.95rem;
  margin: 1rem 0 0.5rem;
}

code {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.85em;
}

/* ---------------------------------------------------------------- layout */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(0.6rem + env(safe-area-inset-top)) 1rem 0.6rem;
  background: var(--bar);
  color: var(--bar-text);
}

.brand {
  color: inherit;
  font-weight: 700;
  font-size: 1.1rem;
  text-decoration: none;
  letter-spacing: 0.04em;
}

.conn {
  font-size: 0.8rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.conn::before {
  content: '';
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  background: #9ca3af;
}

.conn[data-state='ok']::before { background: #22c55e; }
.conn[data-state='degraded']::before { background: #f59e0b; }
.conn[data-state='down']::before { background: #ef4444; }

.banner {
  background: var(--warn);
  color: #fff;
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
}

.main {
  max-width: 760px;
  margin: 0 auto;
  padding: 0.75rem;
}

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  display: flex;
  overflow-x: auto;
  background: var(--bar);
  padding-bottom: env(safe-area-inset-bottom);
  scrollbar-width: none;
}

.tabbar a {
  flex: 1 1 auto;
  min-width: 0;
  height: var(--tabbar-h);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 0.15rem;
  white-space: nowrap;
  color: #9ca3af;
  text-decoration: none;
  font-size: 0.72rem;
  border-top: 3px solid transparent;
}

.tabbar a[aria-current] {
  color: var(--bar-text);
  border-top-color: var(--accent);
  font-weight: 600;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem;
  margin-bottom: 0.75rem;
}

details.card > summary {
  cursor: pointer;
  font-weight: 600;
  list-style-position: inside;
}

details.card[open] > summary {
  margin-bottom: 0.75rem;
}

details > summary {
  cursor: pointer;
  color: var(--accent);
  margin: 0.4rem 0;
}

.quick {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

@media (min-width: 560px) {
  .quick { grid-template-columns: repeat(4, 1fr); }
}

.toolbar {
  margin-bottom: 0.75rem;
}

/* ---------------------------------------------------------------- buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.6rem;
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:disabled {
  opacity: 0.6;
  cursor: progress;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.btn.danger {
  color: var(--danger);
  border-color: var(--danger);
}

.btn.small {
  min-height: 2.25rem;
  padding: 0.3rem 0.7rem;
  font-size: 0.875rem;
}

.btn.wide {
  width: 100%;
}

.btn.active {
  background: var(--text);
  color: var(--surface);
  border-color: var(--text);
}

.btn.busy::after {
  content: '';
  width: 0.9em;
  height: 0.9em;
  margin-left: 0.5em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.link {
  background: none;
  border: none;
  padding: 0 0.25rem;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  text-decoration: underline;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.segmented {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.6rem;
}

.more-link {
  display: inline-block;
  margin-top: 0.5rem;
  font-size: 0.9rem;
}

/* ---------------------------------------------------------------- forms */

.form {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.form label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.875rem;
  font-weight: 500;
}

.form label.check {
  flex-direction: row;
  align-items: center;
  font-weight: 400;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
}

.row > label {
  flex: 1 1 9rem;
}

.row > label.grow {
  flex: 3 1 12rem;
}

input,
select,
textarea {
  width: 100%;
  min-height: 2.6rem;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-weight: 400;
}

input[type='checkbox'],
input[type='radio'] {
  width: auto;
  min-height: 0;
}

textarea {
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus,
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* 1–10 scale picker */
.scale {
  border: none;
  margin: 0;
  padding: 0;
}

.scale legend {
  font-size: 0.875rem;
  font-weight: 500;
  padding: 0;
  margin-bottom: 0.3rem;
}

.pills {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 3px;
}

.pills label {
  position: relative;
  display: block;
  font-weight: 500;
}

.pills input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.pills span {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 2.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  cursor: pointer;
  user-select: none;
}

.pills input:checked + span {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.pills input:focus-visible + span {
  outline: 2px solid var(--accent);
}

.config-row {
  padding: 0.75rem 0;
  border-top: 1px solid var(--border);
}

.config-row:first-child {
  border-top: none;
  padding-top: 0;
}

.config-row > .btn {
  align-self: flex-start;
}

/* ---------------------------------------------------------------- lists */

.list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.65rem 0;
  border-top: 1px solid var(--border);
}

.item:first-child {
  border-top: none;
}

.list.card .item {
  flex-direction: column;
}

.item-main {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-width: 0;
  flex: 1;
  color: inherit;
  text-decoration: none;
}

.item-title {
  font-weight: 600;
  margin: 0;
  overflow-wrap: anywhere;
}

.done {
  text-decoration: line-through;
  color: var(--muted);
}

.directive.inactive .item-title {
  color: var(--muted);
}

.item-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: flex-end;
  flex-shrink: 0;
}

@media (max-width: 420px) {
  .item.directive {
    flex-direction: column;
  }
  .item.directive .item-actions {
    justify-content: flex-start;
  }
}

.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

.chip {
  font-size: 0.8rem;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
}

.timeline {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
}

.timeline li {
  display: flex;
  flex-direction: column;
  padding: 0.45rem 0 0.45rem 0.75rem;
  border-left: 2px solid var(--border);
}

.facts {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.2rem 0.75rem;
  margin: 0.5rem 0;
  font-size: 0.875rem;
}

.facts dt {
  color: var(--muted);
}

.facts dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.plain {
  margin: 0.25rem 0;
  padding-left: 1.1rem;
  font-size: 0.9rem;
}

.plain li {
  margin: 0.25rem 0;
}

/* ---------------------------------------------------------------- status badges */

.badge {
  display: inline-block;
  padding: 0.05rem 0.45rem;
  border-radius: 999px;
  border: 1px solid currentColor;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.5;
  color: var(--neutral);
  text-decoration: none;
  white-space: nowrap;
}

.st-pending { color: var(--neutral); }
.st-in_progress { color: var(--info); }
.st-active { color: var(--warn); }
.st-acknowledged { color: var(--info); }
.st-completed { color: var(--ok); }
.st-failed { color: var(--danger); }
.st-cancelled,
.st-expired { color: var(--muted); border-style: dashed; }

.prio.p1 { color: var(--danger); }
.prio.p2 { color: var(--warn); }

.overdue {
  color: var(--danger);
  font-weight: 600;
}

/* ---------------------------------------------------------------- text & states */

.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.pre { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0.25rem 0; }
.clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.empty { margin: 0.25rem 0; }

.loading::before {
  content: '';
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  margin-right: 0.5em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -0.1em;
}

.error-box {
  border: 1px solid var(--danger);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  color: var(--danger);
}

.error-box p {
  margin: 0 0 0.5rem;
}

.error-text {
  color: var(--danger);
}

.notice {
  background: var(--surface);
  border-left: 4px solid var(--accent);
  padding: 0.6rem 0.75rem;
  border-radius: 6px;
}

pre.code {
  max-height: 50vh;
  overflow: auto;
  padding: 0.6rem;
  background: var(--bg);
  border-radius: 6px;
  font-size: 0.78rem;
}

/* ---------------------------------------------------------------- toasts */

.toasts {
  position: fixed;
  left: 0.75rem;
  right: 0.75rem;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 0.75rem);
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  pointer-events: none;
}

.toast {
  max-width: 32rem;
  padding: 0.6rem 0.9rem;
  border-radius: 8px;
  background: #111827;
  color: #f9fafb;
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.25);
  font-size: 0.9rem;
}

.toast.error {
  background: #b91c1c;
}
