:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #12151b;
  --border: #262b36;
  --text: #e6e9ef;
  --muted: #8b93a7;
  --accent: #4f9cf9;
  --ok: #3ecf8e;
  --warn: #f2b544;
  --err: #f2686b;
  --page-max: 1850px;
}

html { color-scheme: dark; }
html[data-theme="light"] {
  color-scheme: light;
  --bg: #f3f5f9;
  --panel: #ffffff;
  --panel-2: #f7f9fc;
  --border: #d8dfe9;
  --text: #1b2430;
  --muted: #667085;
  --accent: #2563eb;
  --ok: #0f7b52;
  --warn: #a15c00;
  --err: #c0262d;
}

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

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: radial-gradient(1200px 600px at 50% -20%, #1b2333 0%, var(--bg) 55%);
  color: var(--text);
  font: 15px/1.6 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
}

body.login { display: grid; place-items: center; padding: 24px; }
body.portal { background: var(--bg); padding-bottom: 48px; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .92em;
  color: var(--text);
}

.card {
  width: min(620px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .45);
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  border-radius: 999px;
  padding: 4px 12px;
}
.badge.ok { color: var(--ok); background: rgba(62, 207, 142, .1); border: 1px solid rgba(62, 207, 142, .25); }
.badge.warn { color: var(--warn); background: rgba(242, 181, 68, .1); border: 1px solid rgba(242, 181, 68, .25); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }

h1 { margin: 20px 0 6px; font-size: 26px; letter-spacing: -.02em; }
h1 span { color: var(--accent); }
h2 { margin: 0; font-size: 19px; letter-spacing: -.01em; }
h3 { margin: 0 0 4px; font-size: 16px; }
p.sub { margin: 0; color: var(--muted); overflow-wrap: anywhere; }
.muted { color: var(--muted); font-size: 13px; }
.hint { margin: 16px 0 0; color: var(--muted); font-size: 12.5px; }

.notice {
  margin-top: 18px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid rgba(242, 181, 68, .3);
  background: rgba(242, 181, 68, .07);
  color: var(--warn);
  font-size: 13px;
}
.notice.err {
  border-color: rgba(242, 104, 107, .3);
  background: rgba(242, 104, 107, .07);
  color: var(--err);
}
.notice code { color: var(--text); }

.card form { margin-top: 26px; }
.field { margin-bottom: 16px; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field-label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
label.check {
  display: flex; align-items: center; gap: 8px;
  margin: 8px 0 6px; color: var(--text); cursor: pointer;
}
label.check input { width: auto; margin: 0; flex: none; }
.input-row { display: flex; gap: 10px; }
.prefix {
  display: grid; place-items: center;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

input, select {
  flex: 1;
  min-width: 0;
  padding: 13px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: .04em;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
select { letter-spacing: 0; font-family: inherit; }
input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 156, 249, .15);
}
input::placeholder { color: #5d6474; letter-spacing: 0; }

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 13px 18px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #08111f;
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
  white-space: nowrap;
  transition: filter .15s, opacity .15s, background .15s;
}
button:hover:not(:disabled), .btn:hover { filter: brightness(1.1); text-decoration: none; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.ghost, .btn.ghost {
  background: #222836;
  color: var(--text);
  border: 1px solid var(--border);
}
button.danger { color: var(--err); }
button.small { padding: 8px 12px; font-size: 13px; }
.btn { padding: 9px 14px; font-size: 14px; }

.verify-block { margin-top: 8px; padding-top: 22px; border-top: 1px dashed var(--border); }

.status { margin-top: 16px; font-size: 14px; min-height: 22px; white-space: pre-line; }
.status.ok { color: var(--ok); }
.status.err { color: var(--err); }
.status.info { color: var(--muted); }

.success {
  margin-top: 14px;
  padding: 14px 16px;
  border-radius: 10px;
  border: 1px solid rgba(62, 207, 142, .3);
  background: rgba(62, 207, 142, .08);
  color: var(--ok);
  font-weight: 600;
  font-size: 14px;
}

.flow {
  margin-top: 26px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--muted);
}
.node { padding: 4px 10px; border: 1px solid var(--border); border-radius: 7px; background: var(--panel-2); }
footer { margin-top: 14px; font-size: 12px; color: var(--muted); }

/* --- portal page --------------------------------------------------------- */
.topbar {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 28px 24px 18px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  flex-wrap: wrap;
}
.topbar h1 { margin: 0 0 4px; }
.topbar .sub { font-size: 13.5px; }

.theme-toggle {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--text);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
}
.theme-toggle:hover:not(:disabled) {
  background: var(--panel);
  filter: none;
}
.theme-toggle:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.theme-toggle .theme-icon { display: none; }
.theme-toggle .theme-moon { display: block; }
html[data-theme="light"] .theme-toggle .theme-moon { display: none; }
html[data-theme="light"] .theme-toggle .theme-sun { display: block; }
.login-head {
  display: flex;
  justify-content: flex-end;
  margin: 0 0 8px;
}

.wrap { max-width: var(--page-max); margin: 0 auto; padding: 0 24px; }

.section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px;
  margin-top: 18px;
}
.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.section-head .muted { font-size: 12.5px; }
.section h2 { margin-bottom: 0; }

.ops-grid {
  display: grid;
  grid-template-columns: minmax(260px, .9fr) minmax(0, 1.5fr) minmax(300px, 1fr);
  gap: 14px;
  align-items: stretch;
}
.ops-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
}
.ops-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.ops-head h3 { margin: 0; font-size: 16px; }
.fin-brief-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 330px;
  overflow-y: auto;
  padding-right: 2px;
}
.fin-brief-item {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
}
.fin-brief-item.current { border-color: var(--accent); }
.fin-brief-item.error { border-color: var(--err); }
.fin-brief-title { font-size: 13.5px; font-weight: 600; }
.fin-brief-summary {
  margin-top: 4px;
  font-size: 12.5px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}
.fin-brief-time {
  margin-top: 6px;
  color: var(--muted);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}
.metric-card {
  min-width: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
}
.metric-label { color: var(--muted); font-size: 12px; letter-spacing: .08em; }
.metric-value {
  margin-top: 4px;
  font-size: 24px;
  font-weight: 700;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}
.metric-bar {
  height: 6px;
  margin-top: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: #242a35;
}
.metric-bar span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width .35s ease, background .35s ease;
}
.metric-bar span.ok { background: var(--ok); }
.metric-bar span.warn { background: var(--warn); }
.metric-bar span.err { background: var(--err); }
.metric-sub {
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}
.ops-foot {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}
.sessions-list { display: flex; flex-direction: column; gap: 8px; }
.session-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
}
.session-main { min-width: 0; }
.session-ip {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 14px;
}
.session-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}
.session-row button { flex: none; }
.ops-status { min-height: 18px; margin-top: 10px; font-size: 13px; white-space: pre-line; }
.ops-status.ok { color: var(--ok); }
.ops-status.err { color: var(--err); }
.ops-status.info { color: var(--muted); }

@media (max-width: 1100px) {
  .ops-grid { grid-template-columns: 1fr; }
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
}
.item-card {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
}
.item-main { min-width: 0; }
.item-main h3 { overflow-wrap: anywhere; }
.item-title-row {
  display: flex;
  align-items: center;
  gap: 5px;
  min-width: 0;
}
.item-title-row h3 { margin: 0; min-width: 0; }
.drag-handle {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  letter-spacing: -2px;
  cursor: grab;
  user-select: none;
  touch-action: none;
}
.drag-handle:hover { color: var(--text); background: var(--panel); }
.item-card.dragging { opacity: .4; }
.item-card.drop-target { outline: 2px solid var(--accent); outline-offset: 2px; }
.item-main .btn { margin-top: 10px; }
.item-card .muted { margin: 0; overflow-wrap: anywhere; }
.empty { margin: 0; color: var(--muted); font-size: 14px; }

.unit-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}
.unit-actions button { white-space: nowrap; }

.add-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0 16px;
  align-items: start;
}
.add-form .field { margin-bottom: 14px; }
.add-form .field > input,
.add-form .field > select { width: 100%; }
.add-form .muted { overflow-wrap: anywhere; }
.add-form .span-all { grid-column: 1 / -1; }
.add-form > button { grid-column: 1 / -1; justify-self: start; margin-bottom: 14px; }

.kind-switch {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
}
.kind-option { position: relative; display: block; margin: 0; }
.kind-option input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  border: 0;
  margin: 0;
  opacity: 0;
  pointer-events: none;
}
.kind-option span {
  display: block;
  padding: 9px 16px;
  border-radius: 8px;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.kind-option input:checked + span {
  background: var(--accent);
  color: #08111f;
  font-weight: 600;
}
.kind-option input:focus-visible + span {
  box-shadow: 0 0 0 3px rgba(79, 156, 249, .25);
}

/* The conditional wrappers only group fields for show/hide.  display: contents
   lets the name, internal port, access mode and path share one grid row. */
.add-form .service-only,
.add-form .link-only { display: contents; }
.add-form .service-only[hidden],
.add-form .link-only[hidden] { display: none; }
.add-form .link-url { grid-column: span 3; }

/* --- logs page ----------------------------------------------------------- */
.log-controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0 12px;
  align-items: end;
}
.log-controls .field { margin-bottom: 12px; }
.log-controls .field > input,
.log-controls .field > select { width: 100%; }
.log-controls > button { justify-self: start; margin-bottom: 12px; }
.log-controls .log-search { grid-column: span 2; }
.log-auto {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}
.log-auto input { width: auto; margin: 0; flex: none; }
.log-summary {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 12.5px;
  margin: 4px 0 10px;
}
.log-list { display: flex; flex-direction: column; gap: 8px; }
.log-entry {
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--muted);
  border-radius: 8px;
  background: var(--panel-2);
}
.log-entry.level-info { border-left-color: var(--accent); }
.log-entry.level-notice { border-left-color: #7dd3fc; }
.log-entry.level-warning { border-left-color: var(--warn); }
.log-entry.level-err,
.log-entry.level-crit,
.log-entry.level-alert,
.log-entry.level-emerg { border-left-color: var(--err); }
.log-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  min-width: 0;
}
.log-time {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11.5px;
}
.log-badge {
  flex: none;
  padding: 1px 7px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .04em;
}
.log-badge.level-info { color: var(--accent); border-color: rgba(79, 156, 249, .4); }
.log-badge.level-notice { color: #7dd3fc; border-color: rgba(125, 211, 252, .3); }
.log-badge.level-warning { color: var(--warn); border-color: rgba(242, 181, 68, .4); }
.log-badge.level-err,
.log-badge.level-crit,
.log-badge.level-alert,
.log-badge.level-emerg { color: var(--err); border-color: rgba(242, 104, 107, .4); }
.log-source {
  min-width: 0;
  color: var(--muted);
  font-size: 11.5px;
  overflow-wrap: anywhere;
}
.log-message {
  margin: 7px 0 0;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.logs-page .status { margin-top: 10px; }

@media (max-width: 700px) {
  .log-controls .log-search { grid-column: 1 / -1; }
}

@media (max-width: 1280px) {
  .add-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .add-form .link-url { grid-column: span 1; }
}

@media (max-width: 640px) {
  .add-form { grid-template-columns: 1fr; }
  .add-form .link-url { grid-column: 1 / -1; }
}

@media (max-width: 560px) {
  /* Prevent iOS Safari from zooming into a focused 15px input. */
  input, select { font-size: 16px; }
  .add-form > button { width: 100%; }
  .card { padding: 24px; border-radius: 14px; }
  .section { padding: 18px; }
  .topbar { padding: 22px 18px 12px; }
  .wrap { padding: 0 18px; }
  .input-row { flex-wrap: wrap; }
  .prefix { padding: 11px 12px; }
  .input-row button { width: 100%; }
}

/* --- light theme --------------------------------------------------------- */
html[data-theme="light"] body {
  background: radial-gradient(1200px 600px at 50% -20%, #e9effd 0%, var(--bg) 55%);
}
html[data-theme="light"] body.portal { background: var(--bg); }
html[data-theme="light"] .card { box-shadow: 0 24px 60px rgba(31, 45, 61, .12); }
html[data-theme="light"] button,
html[data-theme="light"] .btn { color: #fff; }
html[data-theme="light"] button.ghost,
html[data-theme="light"] .btn.ghost { color: var(--text); background: #eef2f8; }
html[data-theme="light"] button.ghost.danger,
html[data-theme="light"] .btn.ghost.danger { color: var(--err); }
html[data-theme="light"] .theme-toggle { color: var(--text); background: var(--panel-2); }
html[data-theme="light"] .kind-option input:checked + span { color: #fff; }
html[data-theme="light"] .metric-bar { background: #e3e8f1; }
html[data-theme="light"] input::placeholder { color: #8a94a8; }
html[data-theme="light"] .notice { color: #7a4b00; background: #fff8e6; border-color: #efd9a9; }
html[data-theme="light"] .notice.err { color: #a11d24; background: #fff1f2; border-color: #f3c2c6; }
html[data-theme="light"] .badge.ok { background: rgba(15, 123, 82, .08); border-color: rgba(15, 123, 82, .22); }
html[data-theme="light"] .badge.warn { background: rgba(161, 92, 0, .08); border-color: rgba(161, 92, 0, .22); }
html[data-theme="light"] .log-entry.level-notice { border-left-color: #0284c7; }
html[data-theme="light"] .log-badge.level-notice { color: #0369a1; border-color: rgba(3, 105, 161, .25); }
html[data-theme="light"] .bar { background: #e6ebf3; }
html[data-theme="light"] .theme-toggle:hover:not(:disabled) { background: var(--panel); }
