:root {
  --bg: #0b0d0c;
  --panel: #161a18;
  --panel-2: #1d2220;
  --line: #2a302d;
  --text: #e9efec;
  --muted: #8b9690;
  --faint: #5d6762;
  --green: #4dff5f;
  --red: #ff4d4f;
  --amber: #ffb648;
  --blue: #63c6ff;
  --white: #f2f5f3;
  --accent: #3ddc84;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Malgun Gothic", "Noto Sans KR", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); }
body { min-height: 100vh; -webkit-tap-highlight-color: transparent; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--red); min-height: 1.2em; margin: 0; font-size: 14px; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- 로그인 ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 20px; padding: 28px 22px; display: grid; gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card p { margin: 0; font-size: 14px; }
.login-logo img { width: 56px; height: 56px; }
label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
label.check, label.wide { display: flex; align-items: center; gap: 8px; }
input, select {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 12px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

.btn {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 10px;
  padding: 11px 16px; font-weight: 600;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #062312; }
.btn.danger { background: transparent; border-color: #6b2a2b; color: var(--red); }
.btn.small { padding: 7px 12px; font-size: 14px; }

/* ---------- 상단 바 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px; background: rgba(11,13,12,.92);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 18px; flex: none; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--faint); box-shadow: 0 0 0 3px rgba(255,255,255,.04); }
.dot.on { background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.dot.wait { background: var(--amber); }
.dot.off { background: var(--red); }
.tabs { display: flex; gap: 6px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none; border: 1px solid transparent; background: transparent; color: var(--muted);
  padding: 8px 12px; border-radius: 999px; font-weight: 700; font-size: 15px; white-space: nowrap;
}
.tab[aria-selected="true"] { background: var(--panel-2); color: var(--text); border-color: var(--line); }
.actions { display: flex; gap: 4px; position: relative; flex: none; }
.icon-btn { background: transparent; border: 0; width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center; }
.icon-btn:hover { background: var(--panel-2); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: var(--text); stroke-width: 2; stroke-linecap: round; }
#menuBtn svg { fill: var(--text); stroke: none; }
.icon-btn.active { background: var(--accent); }
.icon-btn.active svg { stroke: #062312; }
.menu {
  position: absolute; right: 0; top: 46px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; padding: 6px; min-width: 210px; display: grid; box-shadow: 0 12px 30px rgba(0,0,0,.5);
}
.menu button { text-align: left; background: transparent; border: 0; padding: 10px 12px; border-radius: 8px; }
.menu button:hover { background: var(--panel-2); }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }

.editbar {
  display: flex; align-items: center; gap: 8px; padding: 10px 14px; background: #10261a;
  border-bottom: 1px solid #1f4a31; font-size: 14px; position: sticky; top: 61px; z-index: 9;
}
.editbar span { flex: 1; color: #a7e9c0; }

/* ---------- 페이지/타일 ---------- */
main { padding: 12px 10px calc(24px + env(safe-area-inset-bottom)); max-width: 1500px; margin: 0 auto; }
.page { display: none; }
.page.show { display: block; }
.page-title { display: none; margin: 18px 6px 10px; font-size: 15px; color: var(--muted); font-weight: 700; letter-spacing: .02em; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }

.tile {
  position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  min-height: 180px; padding: 12px 10px 10px; display: flex; flex-direction: column; align-items: center;
  text-align: center; overflow: hidden; transition: border-color .3s, opacity .3s;
}
.tile.wide { grid-column: span 2; }
.tile-title { font-weight: 700; color: #c9d1cc; font-size: 17px; line-height: 1.25; }
.tile-value {
  flex: 1; display: flex; align-items: center; justify-content: center; white-space: pre-line;
  font-weight: 700; line-height: 1.25; padding: 8px 2px; word-break: break-word; font-variant-numeric: tabular-nums;
}
.tile-value.size-m { font-size: clamp(18px, 5.2vw, 26px); }
.tile-value.size-l { font-size: clamp(24px, 7.5vw, 38px); }
.tile-value.size-s { font-size: clamp(14px, 4vw, 18px); }
.tile-value.empty { color: var(--faint) !important; font-size: 16px; font-weight: 500; }
.c-green { color: var(--green); } .c-red { color: var(--red); } .c-amber { color: var(--amber); }
.c-blue { color: var(--blue); } .c-white { color: var(--white); }
.tile-time { font-size: 13px; color: var(--faint); }
.tile.flash { border-color: rgba(77,255,95,.55); }
.tile.stale .tile-value { opacity: .45; }
.tile.stale .tile-time { color: var(--amber); }

.tile-buttons { display: flex; gap: 6px; width: 100%; margin: 4px 0 6px; }
.tile-buttons .send {
  flex: 1; border: 1px solid #3a2224; background: #241416; color: #ffd7d7; border-radius: 10px;
  padding: 12px 6px; font-weight: 800; font-size: 16px;
}
.tile-buttons .send:active { background: #3a1d20; }
.tile-buttons .send.sent { border-color: var(--accent); color: var(--accent); }

.editing .tile { cursor: pointer; border-style: dashed; border-color: #2f6b47; }
.editing .tile::after {
  content: "✎"; position: absolute; top: 8px; right: 10px; color: var(--accent); font-size: 16px;
}
.editing .tile-buttons .send { pointer-events: none; opacity: .6; }
.add-tile {
  border: 1px dashed #2f6b47; background: transparent; color: var(--accent); border-radius: var(--radius);
  min-height: 180px; font-size: 16px; font-weight: 700;
}

/* ---------- 대화상자 ---------- */
.dialog {
  border: 1px solid var(--line); border-radius: 18px; background: var(--panel); color: var(--text);
  padding: 0; width: min(520px, calc(100vw - 24px)); max-height: calc(100vh - 40px);
}
.dialog::backdrop { background: rgba(0,0,0,.6); }
.dialog form { padding: 20px; display: grid; gap: 14px; }
.dialog h2 { margin: 0; font-size: 19px; }
.dialog p { margin: 0; }
fieldset { border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: grid; gap: 10px; margin: 0; }
legend { color: var(--muted); font-size: 14px; padding: 0 6px; }
.btn-row {
  display: grid; grid-template-columns: 1fr 90px auto; gap: 6px; align-items: center;
  padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
.btn-row input { width: 100%; padding: 9px; font-size: 14px; }
.btn-row .b-label { order: 1; }
.btn-row .b-payload { order: 2; }
.btn-row .rm { order: 3; background: transparent; border: 0; color: var(--red); font-size: 20px; padding: 6px; }
.btn-row .b-topic { order: 4; grid-column: 1 / 3; }
.btn-row .b-pick { order: 5; }
.btn-row .cf { order: 6; grid-column: 1 / -1; display: flex; align-items: center; gap: 6px; font-size: 13px; }
.btn-row .cf input { width: 18px; padding: 0; flex: none; }
.dialog-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

#confirmBody { white-space: pre-line; line-height: 1.5; }
.k-topic { display: grid; gap: 6px; }
.k-topic > label, .k-rule > label { color: var(--muted); font-size: 14px; }
.topic-field { display: flex; gap: 6px; }
.topic-field input { flex: 1; }
.topic-field .pick { flex: none; }
.topic-preview { font-size: 13px; color: var(--muted); white-space: pre-line; line-height: 1.45; }
.topic-preview:empty { display: none; }
.topic-preview b { color: var(--green); font-weight: 700; }
.pick-list { display: grid; gap: 6px; max-height: 55vh; overflow-y: auto; }
.pick-item {
  text-align: left; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px;
  display: grid; gap: 4px;
}
.pick-item:hover, .pick-item:focus { border-color: var(--accent); outline: none; }
.pick-item .pt { font-weight: 700; color: var(--blue); font-size: 14px; word-break: break-all; }
.pick-item .pv { color: var(--green); font-size: 13px; white-space: pre-line; max-height: 3.9em; overflow: hidden; }
.pick-item .pa { color: var(--faint); font-size: 12px; }
.pick-item .used { color: var(--amber); font-size: 12px; }
.pick-group { color: var(--muted); font-size: 13px; font-weight: 700; margin: 6px 2px 0; }
.hint { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.5; }
.hint code { color: var(--blue); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.kind-pick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.kind-pick label { display: block; }
.kind-pick input { position: absolute; opacity: 0; pointer-events: none; }
.kind-pick span {
  display: grid; gap: 2px; text-align: center; padding: 10px 6px; border: 1px solid var(--line);
  border-radius: 12px; background: var(--bg); cursor: pointer;
}
.kind-pick b { color: var(--text); font-size: 15px; }
.kind-pick small { color: var(--muted); font-size: 12px; }
.kind-pick input:checked + span { border-color: var(--accent); background: #10261a; }
.kind-pick input:checked + span b { color: var(--accent); }
/* 종류별로 필요한 칸만 보이기 */
#tileForm[data-kind="status"] .k-buttons,
#tileForm[data-kind="status"] .k-confirm,
#tileForm[data-kind="button"] .k-topic,
#tileForm[data-kind="button"] .k-rule,
#tileForm[data-kind="button"] .k-confirm { display: none; }

/* 판단 상태 / 응답 확인 표시 */
.tile-state { font-weight: 800; font-size: 15px; margin-top: 6px; padding: 3px 10px; border-radius: 999px; background: #222; color: var(--muted); }
.tile-state.on { color: #062312; background: var(--green); }
.tile-state.off { color: var(--text); background: #2b3230; }
.tile-fb { font-size: 13px; font-weight: 700; margin-bottom: 4px; }
.tile-fb.wait { color: var(--amber); }
.tile-fb.ok { color: var(--green); }
.tile-fb.fail { color: var(--red); }

.toast {
  position: fixed; left: 50%; bottom: calc(22px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #1f2a24; border: 1px solid #2f6b47; color: #c9f5da; padding: 12px 18px; border-radius: 12px;
  font-weight: 600; z-index: 50; box-shadow: 0 10px 30px rgba(0,0,0,.5); max-width: calc(100vw - 32px);
}

/* ---------- 큰 화면 ---------- */
@media (min-width: 700px) {
  .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
  main { padding: 18px 18px 30px; }
}
@media (min-width: 1100px) {
  .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .tile { min-height: 200px; }
  .tile-value.size-m { font-size: 26px; } .tile-value.size-l { font-size: 38px; } .tile-value.size-s { font-size: 18px; }
}
/* "모두" 탭: 모든 페이지를 한 화면에 촘촘하게 */
body.overview .page { display: block; }
body.overview .page + .page { margin-top: 6px; }
body.overview .page-title {
  display: flex; align-items: center; gap: 8px; margin: 14px 4px 8px; padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}
body.overview .page:first-child .page-title { margin-top: 2px; }
.page-link { background: none; border: 0; padding: 0; color: var(--text); font-size: 15px; font-weight: 800; }
.page-link::after { content: " ›"; color: var(--faint); }
.page-count { color: var(--faint); font-size: 12px; font-weight: 600; }
body.overview .grid { gap: 6px; }
body.overview .tile { min-height: 112px; padding: 8px 6px 6px; border-radius: 12px; }
body.overview .tile-title { font-size: 13px; }
body.overview .tile-value { padding: 4px 2px; }
body.overview .tile-value.size-m { font-size: clamp(14px, 4.2vw, 18px); }
body.overview .tile-value.size-l { font-size: clamp(18px, 5.5vw, 24px); }
body.overview .tile-value.size-s { font-size: clamp(12px, 3.6vw, 15px); }
body.overview .tile-value.empty { font-size: 13px; }
body.overview .tile-time { font-size: 11px; }
body.overview .tile-state { font-size: 12px; margin-top: 4px; padding: 2px 8px; }
body.overview .tile-buttons { margin: 2px 0 4px; gap: 4px; }
body.overview .tile-buttons .send { padding: 8px 4px; font-size: 13px; border-radius: 8px; }
body.overview .tile-fb { font-size: 11px; }
@media (min-width: 700px) {
  body.overview .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
@media (min-width: 1100px) {
  body.overview .grid { grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; }
  body.overview .tile { min-height: 128px; }
  body.overview .tile-value.size-m { font-size: 18px; }
  body.overview .tile-value.size-l { font-size: 24px; }
  body.overview .tile-value.size-s { font-size: 14px; }
}
@media (min-width: 1100px) { body.overview main { max-width: 1500px; } }
@media (min-width: 1100px) {
  main { max-width: 1280px; }
}
.small-note { font-size: 12px !important; color: var(--faint) !important; }
.login-card b { color: var(--text); }
