/* 문자 릴레이 웹 콘솔 — 라이트/다크 토큰 */
:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dfe3e8;
  --text: #16191d;
  --text-2: #5b6470;
  --text-3: #8a929c;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --accent-soft: #e6eefd;
  --ok: #1f9d55;
  --ok-soft: #e3f5ea;
  --warn: #c77700;
  --warn-soft: #fdf1dc;
  --down: #d93636;
  --down-soft: #fde6e6;
  --code: #0f1720;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, "D2Coding", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f242c;
    --border: #2b323c;
    --text: #e8ebef;
    --text-2: #a4adb8;
    --text-3: #717b87;
    --accent: #5b8ef5;
    --accent-text: #0b0e12;
    --accent-soft: #1c2940;
    --ok: #3ccf7b;
    --ok-soft: #133021;
    --warn: #f0a531;
    --warn-soft: #36280f;
    --down: #ff6b6b;
    --down-soft: #3a1717;
    --code: #f5f7fa;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1216;
  --surface: #171b21;
  --surface-2: #1f242c;
  --border: #2b323c;
  --text: #e8ebef;
  --text-2: #a4adb8;
  --text-3: #717b87;
  --accent: #5b8ef5;
  --accent-text: #0b0e12;
  --accent-soft: #1c2940;
  --ok: #3ccf7b;
  --ok-soft: #133021;
  --warn: #f0a531;
  --warn-soft: #36280f;
  --down: #ff6b6b;
  --down-soft: #3a1717;
  --code: #f5f7fa;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

button, input, select, textarea { font: inherit; color: inherit; }

.hidden { display: none !important; }

/* ---------- 레이아웃 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 960px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand .who { font-weight: 500; color: var(--text-2); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }
.tabs {
  max-width: 960px; margin: 0 auto; padding: 0 12px;
  display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  border: 0; background: none; padding: 10px 12px; cursor: pointer;
  color: var(--text-2); font-weight: 600; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }

main { max-width: 960px; margin: 0 auto; padding: 16px; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.tiny { font-size: 12px; color: var(--text-3); }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px; min-width: 0;
}
.card h3 { margin: 0 0 6px; font-size: 15px; }
.section-title { font-size: 13px; font-weight: 700; color: var(--text-2); margin: 18px 0 8px; text-transform: none; }

/* ---------- 컨트롤 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 12px; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: 14px;
  min-height: 36px; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--down); border-color: var(--down); background: transparent; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 8px; min-height: 30px; font-size: 13px; }

.input, select.input, textarea.input {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px;
  padding: 7px 10px; min-height: 36px; width: 100%; min-width: 0;
}
textarea.input { min-height: 80px; resize: vertical; font-family: var(--mono); font-size: 13px; }
.input:focus, .btn:focus-visible, .tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); min-width: 0; }
label.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer; }

/* ---------- 로그인 ---------- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login { width: 100%; max-width: 380px; }
.login h1 { font-size: 22px; margin: 0 0 4px; }
.code-input {
  font-family: var(--mono); font-size: 22px; letter-spacing: 2px; text-align: center; text-transform: uppercase;
}
.error-text { color: var(--down); font-size: 14px; min-height: 20px; }

/* ---------- 배지 ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 700; background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.down { background: var(--down-soft); color: var(--down); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--text-3); }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); } .dot.down { background: var(--down); }

/* ---------- 수신함 ---------- */
.inbox-toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.msg { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; transition: opacity 0.4s; }
.msg .label { font-weight: 700; font-size: 14px; color: var(--text-2); overflow-wrap: anywhere; }
.msg .code {
  font-family: var(--mono); font-size: 30px; font-weight: 700; letter-spacing: 3px; color: var(--code);
  overflow-wrap: anywhere; line-height: 1.2;
}
.msg .code.nocode { font-family: inherit; font-size: 15px; font-weight: 500; letter-spacing: 0; }
.msg .meta { grid-column: 1 / -1; display: flex; gap: 6px; flex-wrap: wrap; font-size: 13px; color: var(--text-2); }
.msg .actions { grid-row: 1 / span 2; grid-column: 2; display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
.msg details { grid-column: 1 / -1; font-size: 13px; }
.msg details summary { cursor: pointer; color: var(--text-3); }
.msg pre { white-space: pre-wrap; overflow-wrap: anywhere; margin: 6px 0 0; font-family: inherit; background: var(--surface-2); padding: 8px 10px; border-radius: 8px; }
.msg.old { opacity: 0.45; }
.msg.fresh { animation: fresh 2.4s ease-out; border-color: var(--accent); }
@keyframes fresh {
  0% { background: var(--accent-soft); transform: scale(1.01); }
  100% { background: var(--surface); transform: scale(1); }
}
.empty { text-align: center; color: var(--text-3); padding: 40px 16px; }
.conn { font-size: 12px; color: var(--text-3); display: inline-flex; align-items: center; gap: 6px; }

/* ---------- 기기 ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: 13px; margin-top: 8px; }
.kv dt { color: var(--text-3); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.issues { margin: 6px 0 0; padding-left: 18px; font-size: 13px; }
.issues li { color: var(--warn); }
.issues li.down { color: var(--down); }

/* ---------- 규칙 편집기 ---------- */
.rule { display: flex; flex-direction: column; gap: 10px; }
.rule.disabled { opacity: 0.6; }
.rule-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.rule-head .title { font-weight: 700; flex: 1; min-width: 120px; overflow-wrap: anywhere; }
.rule-fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.cond-group { border-left: 3px solid var(--accent); padding: 6px 0 6px 10px; display: flex; flex-direction: column; gap: 6px; }
.cond-group.not { border-left-color: var(--down); }
.cond-group.any { border-left-color: var(--warn); }
.cond-leaf { display: grid; grid-template-columns: 110px 120px 1fr auto; gap: 6px; align-items: start; }
.cond-leaf .input { min-height: 32px; padding: 5px 8px; font-size: 14px; }
.cond-leaf textarea.input { min-height: 32px; height: 32px; font-family: inherit; }
.err-list { margin: 6px 0 0; padding-left: 18px; color: var(--down); font-size: 13px; }
.rule.has-error { border-color: var(--down); }
.sticky-actions {
  position: sticky; bottom: 0; background: var(--bg); padding: 10px 0; display: flex; gap: 8px; flex-wrap: wrap;
  border-top: 1px solid var(--border); margin-top: 12px; z-index: 5;
}
.trace { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 8px; }
.trace th, .trace td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--border); }
.trace tr.sel td { background: var(--ok-soft); font-weight: 700; }
.code-box {
  font-family: var(--mono); font-size: 24px; font-weight: 700; letter-spacing: 2px; padding: 10px 14px;
  background: var(--surface-2); border-radius: 8px; display: inline-block; overflow-wrap: anywhere;
}
.notice { background: var(--warn-soft); color: var(--warn); border-radius: 8px; padding: 8px 12px; font-size: 13px; }
.notice.info { background: var(--accent-soft); color: var(--accent); }
.notice.err { background: var(--down-soft); color: var(--down); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2); max-width: calc(100vw - 32px);
}

dialog.modal {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  padding: 18px; width: min(440px, calc(100vw - 32px));
}
dialog.modal::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog.modal h3 { margin: 0 0 8px; }

@media (max-width: 560px) {
  main { padding: 12px; }
  .cond-leaf { grid-template-columns: 1fr 1fr; }
  .cond-leaf > :nth-child(3) { grid-column: 1 / -1; }
  .cond-leaf > :nth-child(4) { grid-column: 1 / -1; justify-self: end; }
  .msg .code { font-size: 26px; letter-spacing: 2px; }
  .brand .who { max-width: 120px; }
}

/* ---------- 앱 설치 QR ---------- */
.tab.tab-action { margin-left: auto; color: var(--accent); }
a.btn { text-decoration: none; }
.login-install { text-align: center; color: var(--text-2); }
.install-modal .qr-box {
  align-self: center; background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 6px; line-height: 0;
}
.qr-canvas { display: block; width: min(260px, 68vw); height: auto; image-rendering: pixelated; }
.qr-url { display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.qr-url code { font-family: var(--mono); font-size: 12px; color: var(--text-2); overflow-wrap: anywhere; min-width: 0; }

@media print {
  html:has(body.print-qr), body.print-qr { background: #fff !important; }
  body.print-qr > *:not(dialog) { display: none !important; }
  body.print-qr dialog.modal {
    position: static; border: none; box-shadow: none; width: 100%; max-width: none; margin: 0; padding: 0;
    max-height: none; height: auto; overflow: visible; background: #fff; color: #000;
  }
  body.print-qr dialog.modal::backdrop { display: none; }
  body.print-qr .no-print { display: none !important; }
  body.print-qr .install-modal { align-items: center; text-align: center; }
  body.print-qr .install-modal .qr-box { border: none; }
  body.print-qr .qr-canvas { width: 80mm; }
  body.print-qr .qr-url code { color: #000; font-size: 11pt; }
}
