/* ============================================================
   LUNA Design System — 사용자 앱 + 관리자 콘솔 공통
   ============================================================ */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0a0d13;
  --bg2: #0e1219;
  --panel: #121722;
  --panel2: #182031;
  --panel3: #1e2839;
  --border: #25304496;
  --border2: #2f3b52;
  --text: #e8ecf4;
  --muted: #8d97ab;
  --muted2: #5d6779;
  --accent: #6f8cff;
  --accent-dim: #4a63b8;
  --accent2: #a37cff;
  --green: #4ade80;
  --yellow: #fbbf24;
  --red: #f87171;
  --blue-info: #60a5fa;
  --grad: linear-gradient(135deg, #6f8cff 0%, #a37cff 100%);
  --radius: 14px;
  --radius-sm: 8px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --font: "Segoe UI", "Pretendard", "Malgun Gothic", -apple-system, sans-serif;
}

html, body { height: 100%; }
body {
  background:
    radial-gradient(1200px 800px at 85% -10%, rgba(111, 140, 255, 0.07), transparent 60%),
    radial-gradient(900px 700px at -10% 110%, rgba(163, 124, 255, 0.06), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 12px; }

/* ---------- 공통 컴포넌트 ---------- */
.btn {
  border: none; border-radius: 10px; padding: 9px 16px;
  font-size: 13px; font-family: var(--font); font-weight: 600;
  cursor: pointer; transition: all .15s ease; color: var(--text);
  background: var(--panel2); border: 1px solid var(--border2);
  white-space: nowrap; flex-shrink: 0;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--grad); border: none; color: #fff; }
.btn.primary:hover { filter: brightness(1.12); }
.btn.primary:disabled { opacity: .4; cursor: not-allowed; filter: none; }
.btn.full { width: 100%; }
.btn.ghost { background: transparent; }
.btn.sm { padding: 5px 10px; font-size: 12px; border-radius: 8px; }
.btn.danger { color: var(--red); border-color: #5b2d33; }
.btn.danger:hover { border-color: var(--red); background: rgba(248, 113, 113, 0.08); }

.icon-btn {
  background: var(--panel2); color: var(--muted);
  border: 1px solid var(--border2); border-radius: 8px;
  padding: 6px 10px; font-size: 12px; cursor: pointer;
  font-family: var(--font); transition: all .15s ease; white-space: nowrap;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent); }
.icon-btn.danger:hover { color: var(--red); border-color: var(--red); }

.badge {
  display: inline-block; font-size: 10px; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border2); color: var(--muted); font-weight: 600; letter-spacing: .3px;
}
.badge.cat { color: var(--accent); border-color: #34406b; background: rgba(111, 140, 255, .08); }
.badge.long { color: var(--green); border-color: #2c5c40; background: rgba(74, 222, 128, .07); }
.badge.mid { color: var(--yellow); border-color: #5c532c; background: rgba(251, 191, 36, .07); }
.badge.short { color: var(--muted); }
.badge.archived { color: var(--red); border-color: #5b2d33; }
.badge.active { color: var(--green); border-color: #2c5c40; }
.badge.ok { color: var(--green); border-color: #2c5c40; }
.badge.fail { color: var(--red); border-color: #5b2d33; }

.section-label {
  font-size: 11px; font-weight: 700; color: var(--muted2);
  text-transform: uppercase; letter-spacing: 1.2px; margin: 18px 4px 8px;
}

.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; transition: border-color .15s ease;
}
.card:hover { border-color: var(--border2); }
.card.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

input[type="text"], input[type="number"], textarea, select {
  background: var(--bg2); border: 1px solid var(--border2); color: var(--text);
  border-radius: 10px; padding: 9px 12px; font-size: 13px; font-family: var(--font);
  outline: none; transition: border-color .15s ease; width: 100%;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
textarea { resize: vertical; line-height: 1.5; }
label.field { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--muted); }

input[type="range"] { accent-color: var(--accent); width: 100%; }

/* ---------- 앱 레이아웃 (사용자) ---------- */
#app { display: grid; grid-template-columns: 290px 1fr; height: 100vh; }

#sidebar {
  background: rgba(14, 18, 26, 0.85); backdrop-filter: blur(8px);
  border-right: 1px solid var(--border);
  padding: 20px 16px; display: flex; flex-direction: column; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.brand.small { margin-bottom: 0; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 12px; background: var(--grad);
  display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: 18px; color: #fff; box-shadow: 0 4px 14px rgba(111, 140, 255, .35);
}
.brand-name { font-size: 16px; font-weight: 800; letter-spacing: 2.5px; }
.brand-sub { font-size: 11px; color: var(--muted); margin-top: 2px; }

.agent-list { display: flex; flex-direction: column; gap: 6px; flex: 1; }
.agent-item {
  display: flex; align-items: center; gap: 11px; padding: 10px 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  cursor: pointer; transition: all .15s ease;
}
.agent-item:hover { border-color: var(--border2); background: var(--panel2); }
.agent-item.active { border-color: var(--accent); background: rgba(111, 140, 255, .07); }
.agent-item .meta { min-width: 0; flex: 1; }
.agent-item .name { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.agent-item .sub { font-size: 11px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.avatar {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px; color: #fff; background: var(--grad);
}
.avatar.ghost { background: var(--panel3); color: var(--muted); }

.sidebar-foot { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border); }
.admin-link { color: var(--muted); font-size: 12px; text-decoration: none; }
.admin-link:hover { color: var(--text); }

/* 사용자 칩/게이트 */
.user-chip {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 8px 10px; margin-bottom: 12px;
}
.user-chip .meta { flex: 1; min-width: 0; }
.user-chip .name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-chip .sub { font-size: 10.5px; color: var(--muted2); margin-top: 1px; }

#user-gate {
  position: fixed; inset: 0; z-index: 200;
  background:
    radial-gradient(900px 600px at 80% -10%, rgba(111, 140, 255, 0.12), transparent 60%),
    var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.gate-card {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  text-align: center; max-width: 380px; width: 100%;
}
.gate-card h1 { font-size: 26px; letter-spacing: 4px; }
.gate-card p { color: var(--muted); line-height: 1.7; font-size: 13px; }
.gate-card .muted { font-size: 11.5px; }
.brand-mark.big { width: 72px; height: 72px; border-radius: 22px; font-size: 34px; box-shadow: 0 12px 40px rgba(111, 140, 255, .35); }
#gate-form { display: flex; gap: 8px; width: 100%; margin-top: 6px; }
#gate-form input[type="text"] { flex: 1; width: auto; min-width: 0; }
#gate-form .btn { flex-shrink: 0; align-self: stretch; }

/* ---------- 메인/빈 상태 ---------- */
#main { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
#empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: 14px; padding: 40px;
}
.empty-mark {
  width: 72px; height: 72px; border-radius: 22px; background: var(--grad);
  display: flex; align-items: center; justify-content: center;
  font-size: 34px; font-weight: 800; color: #fff;
  box-shadow: 0 12px 40px rgba(111, 140, 255, 0.35); margin-bottom: 8px;
}
#empty-state h1 { font-size: 26px; font-weight: 700; }
#empty-state p { color: var(--muted); line-height: 1.7; max-width: 420px; }
#empty-state b { color: var(--text); }

/* ---------- 채팅 ---------- */
#chat-view { display: flex; flex-direction: column; height: 100vh; }
#chat-header {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 14px 22px; border-bottom: 1px solid var(--border);
  background: rgba(14, 18, 26, 0.75); backdrop-filter: blur(8px);
}
.chat-title-wrap { display: flex; align-items: center; gap: 12px; min-width: 0; }
#chat-title { font-weight: 700; font-size: 15px; }
#chat-sub { margin-top: 1px; }
.chat-actions { display: flex; gap: 6px; align-items: center; }
.ctx-usage { font-size: 11px; color: var(--muted2); margin-right: 6px; }

.font-pop-wrap { position: relative; }
.font-pop {
  position: absolute; top: calc(100% + 8px); right: 0; z-index: 50;
  background: var(--bg2); border: 1px solid var(--border2); border-radius: 12px;
  box-shadow: var(--shadow); padding: 14px;
  min-width: 230px;
}
.font-slider-row { display: flex; align-items: center; gap: 10px; }
.font-slider-row input[type="range"] { flex: 1; accent-color: var(--accent); }
.font-endpoint { color: var(--muted); font-size: 11px; }
.font-endpoint.big { font-size: 16px; color: var(--text); }
.font-slider-meta {
  display: flex; justify-content: space-between; align-items: center; margin-top: 8px;
}
.font-slider-meta #font-val { color: var(--text); font-size: 12px; font-weight: 600; }
.font-slider-meta #font-reset {
  background: none; border: none; color: var(--muted); font-size: 11px;
  cursor: pointer; font-family: var(--font); text-decoration: underline;
}
.font-slider-meta #font-reset:hover { color: var(--text); }

#messages {
  flex: 1; overflow-y: auto; padding: 26px 22px;
  display: flex; flex-direction: column; gap: 14px;
}
#messages .day-group {
  align-self: center; font-size: 11px; color: var(--muted);
  background: rgba(255, 255, 255, 0.05); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px 14px; margin: 4px 0 2px;
}

.msg-row { display: flex; gap: 10px; max-width: 780px; width: 100%; margin: 0 auto; }
.msg-row.user { flex-direction: row-reverse; }
.msg-bubble {
  padding: 11px 15px; border-radius: 16px; line-height: 1.6; white-space: pre-wrap; word-break: break-word;
  font-size: var(--chat-font-size, 13.5px);
}
.msg-time { font-size: calc(var(--chat-font-size, 13.5px) * 0.75); color: var(--muted2); margin-top: 4px; }
.msg-row.assistant .msg-bubble { background: var(--panel); border: 1px solid var(--border); border-top-left-radius: 4px; }
.msg-row.user .msg-bubble {
  background: linear-gradient(135deg, #4a63b8, #5b4ea8); color: #fff;
  border-top-right-radius: 4px;
}
.msg-row .avatar { width: 30px; height: 30px; font-size: 12px; margin-top: 2px; }
.msg-row.user .avatar { background: var(--panel3); color: var(--muted); }
.msg-row.user .msg-time { text-align: right; }
.msg-pending { opacity: .55; font-style: italic; }

.proactive-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 18px; height: 18px; border-radius: 50%;
  background: rgba(163, 124, 255, 0.14); border: 1px solid #4a3b6b;
  color: var(--accent2); font-size: 10px; margin-bottom: 4px;
}

#composer {
  display: flex; gap: 10px; padding: 0 22px 14px;
  border-top: 1px solid var(--border);
  background: rgba(14, 18, 26, 0.75);
  align-items: flex-end; /* 입력칸이 커져도 전송 버튼은 하단 유지 */
  flex-wrap: wrap; /* 상단 리사이즈 바가 한 줄을 차지 */
}
#composer-resize {
  width: 100%; height: 10px;
  display: flex; align-items: center; justify-content: center;
  cursor: row-resize; touch-action: none;
}
#composer-resize::before {
  content: ""; width: 42px; height: 3px; border-radius: 2px;
  background: var(--border2); transition: background .15s ease;
}
#composer-resize:hover::before, #composer-resize.dragging::before { background: var(--accent); }
#composer .input-wrap {
  flex: 1; min-width: 0;
  background: var(--panel2); border: 1px solid var(--border2);
  border-radius: 14px; padding: 6px 6px 6px 16px; display: flex;
  transition: border-color .15s ease;
}
#composer .input-wrap:focus-within { border-color: var(--accent); }
#chat-input {
  flex: 1; background: none; border: none; color: var(--text);
  font-size: 14px; font-family: var(--font); outline: none;
  padding: 9px 6px; line-height: 1.55;
  height: 56px;               /* 기본 높이(기존 44px → 확대) */
  min-height: 40px;
  overflow-y: auto;
}
#chat-send {
  align-self: flex-end;
  background: var(--grad); color: #fff; border: none; border-radius: 12px;
  height: 44px; padding: 0 22px; font-size: 13px; font-weight: 600; cursor: pointer;
  font-family: var(--font); transition: filter .15s ease;
}
#chat-send:hover:not(:disabled) { filter: brightness(1.12); }
#chat-send:disabled { opacity: .35; cursor: not-allowed; }

/* ---------- 모달 ---------- */
#modal-overlay {
  position: fixed; inset: 0; background: rgba(4, 6, 10, 0.72); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 24px;
}
.modal {
  background: var(--bg2); border: 1px solid var(--border2); border-radius: 20px;
  box-shadow: var(--shadow); width: min(680px, 100%); max-height: 86vh;
  display: flex; flex-direction: column; padding: 24px; gap: 14px; overflow-y: auto;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { font-size: 17px; }
.modal-foot { display: flex; gap: 10px; align-items: center; }
.modal-foot input[type="text"] { flex: 1; width: auto; min-width: 0; }
.modal-foot .btn { white-space: nowrap; }
.modal-foot .btn { white-space: nowrap; }

.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.tpl-card {
  background: var(--panel); border: 1.5px solid var(--border); border-radius: 14px;
  padding: 14px; cursor: pointer; transition: all .15s ease; display: flex; flex-direction: column; gap: 8px;
}
.tpl-card:hover { border-color: var(--border2); transform: translateY(-1px); }
.tpl-card.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.tpl-card .t-name { font-weight: 700; font-size: 13.5px; display: flex; align-items: center; gap: 6px; justify-content: space-between; min-width: 0; }
.tpl-card .t-name > span:first-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tpl-card .t-desc { font-size: 12px; color: var(--muted); line-height: 1.55; flex: 1; }
.trait-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.trait-chip {
  font-size: 10px; padding: 2px 7px; border-radius: 999px;
  background: var(--panel3); color: var(--muted); border: 1px solid var(--border);
}

/* ============================================================
   관리자 콘솔
   ============================================================ */
#admin { height: 100vh; display: flex; flex-direction: column; }
.admin-top {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 22px; border-bottom: 1px solid var(--border);
  background: rgba(14, 18, 26, 0.85); backdrop-filter: blur(8px);
}
.admin-body { flex: 1; display: grid; grid-template-columns: 220px 1fr; overflow: hidden; }
.admin-nav {
  border-right: 1px solid var(--border); padding: 18px 12px;
  display: flex; flex-direction: column; gap: 4px; background: rgba(14, 18, 26, 0.6);
}
.nav-btn {
  background: none; border: none; color: var(--muted); text-align: left;
  padding: 10px 14px; border-radius: 10px; font-size: 13px; font-weight: 600;
  cursor: pointer; font-family: var(--font); transition: all .15s ease;
}
.nav-btn:hover { color: var(--text); background: var(--panel2); }
.nav-btn.active { color: #fff; background: rgba(111, 140, 255, 0.13); }

#admin-content { overflow-y: auto; padding: 24px; }
.admin-section h2 { font-size: 17px; margin-bottom: 4px; }
.admin-section > p.desc { color: var(--muted); font-size: 12.5px; margin-bottom: 18px; line-height: 1.6; }
.admin-grid { display: grid; gap: 14px; }

/* 에이전트 모니터링: 마스터-디테일 */
.mon-layout { display: grid; grid-template-columns: 300px 1fr; gap: 16px; align-items: start; }
.mon-list { display: flex; flex-direction: column; gap: 8px; max-height: calc(100vh - 130px); overflow-y: auto; }
.mon-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px; cursor: pointer;
  transition: all .15s ease;
}
.mon-item:hover { border-color: var(--border2); }
.mon-item.selected { border-color: var(--accent); background: rgba(111, 140, 255, .07); }
.mon-item .meta { flex: 1; min-width: 0; }
.mon-item .name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mon-item .sub { font-size: 11px; color: var(--muted); margin-top: 2px; }

.mon-detail { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px;
}
.stat .v { font-size: 20px; font-weight: 700; margin-top: 3px; }
.stat .v small { font-size: 11px; color: var(--muted); font-weight: 500; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; flex-wrap: wrap; }
.tab {
  background: none; border: none; color: var(--muted); padding: 9px 14px;
  font-size: 13px; font-weight: 600; cursor: pointer; font-family: var(--font);
  border-bottom: 2px solid transparent; transition: all .15s ease;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

.kv { display: flex; flex-direction: column; gap: 3px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.kv:last-child { border-bottom: none; }
.kv .k { font-size: 11px; color: var(--muted2); font-weight: 600; }
.kv .v { font-size: 13px; line-height: 1.6; }

.trait-bar-row { display: grid; grid-template-columns: 90px 1fr 42px; align-items: center; gap: 10px; padding: 5px 0; }
.trait-bar-track { height: 7px; background: var(--panel3); border-radius: 4px; overflow: hidden; }
.trait-bar-fill { height: 100%; border-radius: 4px; background: var(--grad); }
.trait-bar-row .val { font-size: 11px; color: var(--muted); text-align: right; }

.mem-row {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 11px 13px; display: flex; flex-direction: column; gap: 6px;
}
.mem-row .head { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.mem-row .content { font-size: 12.5px; line-height: 1.6; }
.mem-row .scores { font-size: 10.5px; color: var(--muted2); display: flex; gap: 10px; flex-wrap: wrap; }

.log-row {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 12px; font-size: 12px; display: flex; flex-direction: column; gap: 3px;
}
.change-item {
  border-left: 2px solid var(--accent2); padding: 4px 0 4px 12px; margin-bottom: 8px;
  font-size: 12.5px; line-height: 1.55;
}

/* 내면·변화 추적 */
.influence-row { gap: 5px; }
.conv-ref {
  color: var(--blue-info); font-size: 11px; border: 1px dashed var(--border2);
  border-radius: 6px; padding: 1px 6px; margin-left: 4px;
}
.fb-btns { display: flex; gap: 6px; margin-top: 2px; }

/* 대화 관리 */
.convo-row .head { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 12px; }
.usage-line { font-size: 11px; color: var(--muted2); }

/* 템플릿 관리 */
.tpl-admin-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.tpl-admin-card { display: flex; flex-direction: column; gap: 10px; }
.tpl-admin-card .head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.tpl-admin-card .t-desc { font-size: 12px; color: var(--muted); line-height: 1.55; }

.traits-editor { display: flex; flex-direction: column; gap: 8px; }
.traits-editor label { display: grid; grid-template-columns: 64px 1fr 40px; align-items: center; gap: 10px; font-size: 12px; color: var(--muted); }
.traits-editor .val { text-align: right; color: var(--text); font-size: 11px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .full { grid-column: 1 / -1; }

/* 운영 설정 */
.ops-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; align-items: start; }
.ops-card { display: flex; flex-direction: column; gap: 12px; }
.ops-row { display: flex; gap: 8px; flex-wrap: wrap; }
.backup-row {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 9px 13px; font-size: 12px;
}

/* 스크롤바 */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--panel3); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--border2); }
::-webkit-scrollbar-track { background: transparent; }
