/* ==========================================================
   SysMon · 시스템 모니터 대시보드
   100명 패널 합의 (2026-04-25): Pretendard, 14px, contain layout,
   고정 슬롯 process_top, 한국어 라벨, topbar 3열 grid
   ========================================================== */

:root {
    /* 배경 */
    --bg: #0b0d11;
    --bg-card: #14171c;
    --bg-card-hover: #181c22;
    --bg-elev: #1d2128;
    --bg-row-alt: #181a1f;

    /* 전경 */
    --fg-strong: #f5f6f8;
    --fg: #e7e8ea;
    --fg-muted: #9aa0a6;
    --fg-subtle: #6a6d74;

    /* 경계 */
    --border: #262a31;
    --border-strong: #2f343c;

    /* 의미 색상 */
    --accent: #4f8cff;
    --ok:    #3ec97b;
    --warn:  #f5b042;
    --crit:  #ff5d5d;
    --info:  #55c2da;

    /* 의미 배경 (저채도) */
    --ok-bg:     rgba(62,201,123,0.14);
    --warn-bg:   rgba(245,176,66,0.16);
    --crit-bg:   rgba(255,93,93,0.16);
    --info-bg:   rgba(85,194,218,0.14);
    --accent-bg: rgba(79,140,255,0.14);

    /* 간격 토큰 */
    --sp-1: 4px; --sp-2: 8px; --sp-3: 12px;
    --sp-4: 16px; --sp-5: 20px; --sp-6: 28px;

    /* 타이포 토큰 */
    --fs-xs: 12px; --fs-sm: 13px; --fs-md: 14px;
    --fs-lg: 16px; --fs-xl: 20px; --fs-2xl: 28px;

    /* 반경 */
    --radius-sm: 6px; --radius-md: 10px; --radius-lg: 14px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
}
body {
    font-family:
        "Pretendard Variable", Pretendard,
        -apple-system, BlinkMacSystemFont, system-ui,
        "Apple SD Gothic Neo", "Noto Sans KR",
        "Segoe UI", sans-serif;
    font-size: var(--fs-md);
    line-height: 1.55;
    letter-spacing: -0.01em;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

.tnum, table.kv td, table.ttable td.num, .value {
    font-variant-numeric: tabular-nums;
    font-feature-settings: "tnum";
}

/* ==========================================================
   로그인 오버레이
   ========================================================== */
.overlay {
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, 0.72);
    backdrop-filter: blur(6px);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
}
.login-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 28px 32px;
    width: 340px;
    display: flex; flex-direction: column; gap: var(--sp-3);
    box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.login-card h1 {
    margin: 0;
    font-size: var(--fs-xl);
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--fg-strong);
}
.login-card .muted { color: var(--fg-muted); margin: 0; font-size: var(--fs-sm); }
.login-card input {
    background: var(--bg-elev); color: var(--fg);
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 10px 12px; font-size: var(--fs-md);
    outline: none; font-family: inherit;
}
.login-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }
.login-card button {
    background: var(--accent); color: #fff; border: 0;
    border-radius: var(--radius-sm);
    padding: 10px 14px; font-size: var(--fs-md); font-weight: 600;
    cursor: pointer; font-family: inherit;
    transition: background .15s;
}
.login-card button:hover { background: #6ba0ff; }
.login-card .error {
    color: var(--crit); margin: 0;
    min-height: 1.2em; font-size: var(--fs-sm);
}

/* ==========================================================
   상단 바 (3열 grid: brand | status | user)
   - .page 와 동일한 max-width 1600px + 좌우 padding 동기화
   ========================================================== */
.topbar {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 50;
}
.topbar-inner {
    max-width: 1600px;
    margin: 0 auto;
    padding: 14px var(--sp-5);
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: var(--sp-5);
    min-height: 64px;
    box-sizing: border-box;
}

.topbar-brand {
    display: flex; align-items: center; gap: var(--sp-2);
}
.brand-mark {
    color: var(--accent);
    font-size: var(--fs-xl);
    line-height: 1;
}
.brand-text {
    font-weight: 700;
    font-size: var(--fs-lg);
    color: var(--fg-strong);
    letter-spacing: -0.015em;
}
.brand-host {
    font-size: var(--fs-xs);
    color: var(--fg-muted);
    padding: 2px 8px;
    background: var(--bg-elev);
    border-radius: var(--radius-sm);
    font-variant-numeric: tabular-nums;
}

.topbar-status {
    display: flex; align-items: center; gap: var(--sp-4);
    justify-self: center;
    color: var(--fg-muted);
    font-size: var(--fs-sm);
}
.status-pill {
    display: inline-flex; align-items: center; gap: 6px;
    padding: 4px 10px;
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: 999px;
    font-size: var(--fs-xs);
    color: var(--fg);
}
.status-pill .dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: var(--fg-subtle);
    transition: background .2s, box-shadow .2s;
}
.status-pill.ok .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.status-pill.warn .dot { background: var(--warn); box-shadow: 0 0 8px var(--warn); }
.status-pill.crit .dot { background: var(--crit); box-shadow: 0 0 8px var(--crit); }
.status-pill.ok { background: var(--ok-bg); border-color: rgba(62,201,123,0.3); color: var(--ok); }
.status-pill.warn { background: var(--warn-bg); border-color: rgba(245,176,66,0.3); color: var(--warn); }
.status-pill.crit { background: var(--crit-bg); border-color: rgba(255,93,93,0.3); color: var(--crit); }

.meta-item {
    display: inline-flex; align-items: center; gap: 6px;
}
.meta-key {
    color: var(--fg-subtle);
    font-size: var(--fs-xs);
}
.meta-item strong {
    color: var(--fg);
    font-weight: 500;
}

.topbar-user {
    display: contents;
}
.btn-logout {
    justify-self: end;
    align-self: center;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    height: 28px;
    padding: 0 12px;
    background: transparent;
    color: var(--fg-muted);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: var(--fs-sm);
    font-family: inherit;
    line-height: 1;
    white-space: nowrap;
    cursor: pointer;
    margin: 0;
    transition: background .15s, color .15s, border-color .15s;
}
.btn-logout > span[aria-hidden] {
    font-size: var(--fs-md);
    line-height: 1;
}
.btn-logout:hover {
    background: var(--crit-bg);
    color: var(--crit);
    border-color: rgba(255,93,93,0.4);
}
.btn-logout:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* ==========================================================
   메인 영역 + 섹션 헤더 + 그리드
   ========================================================== */
.page {
    max-width: 1600px;
    margin: 0 auto;
    padding: var(--sp-4) var(--sp-5) var(--sp-6);
}

.section-title {
    font-size: var(--fs-sm);
    font-weight: 600;
    color: var(--fg-muted);
    margin: var(--sp-5) 0 var(--sp-3);
    padding-left: var(--sp-1);
    border-left: 3px solid var(--accent);
    padding-left: var(--sp-3);
    letter-spacing: -0.005em;
}
.section-title:first-child { margin-top: 0; }

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    grid-auto-rows: minmax(140px, auto);
    grid-auto-flow: row dense;
    gap: var(--sp-3);
    contain: layout;
}

/* ==========================================================
   카드
   ========================================================== */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--sp-4) var(--sp-4);
    min-height: 140px;
    display: flex;
    flex-direction: column;
    contain: layout style;
    transition: border-color .15s;
}
.card:hover { border-color: var(--border-strong); }
.card.span2 { grid-column: span 2; }
@media (max-width: 760px) {
    .grid { grid-template-columns: 1fr; }
    .card.span2 { grid-column: span 1; }
}

.card-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--sp-3);
    margin: 0 0 var(--sp-3);
    padding-bottom: var(--sp-2);
    border-bottom: 1px solid var(--border);
}
.card-title {
    margin: 0;
    font-size: var(--fs-md);
    font-weight: 700;
    color: var(--fg-strong);
    letter-spacing: -0.015em;
}
.card-sub {
    font-size: var(--fs-xs);
    color: var(--fg-subtle);
    font-weight: 400;
}

.sub {
    margin: var(--sp-3) 0 var(--sp-2);
    font-size: var(--fs-xs);
    color: var(--fg-muted);
    font-weight: 600;
}

/* ==========================================================
   메트릭 (값 + 라벨)
   ========================================================== */
.row {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sp-3);
    margin-bottom: var(--sp-3);
}
.metric {
    flex: 1 1 110px;
    padding: 4px 0;
}
.metric .value {
    font-size: var(--fs-xl);
    font-weight: 700;
    line-height: 1.1;
    letter-spacing: -0.02em;
    color: var(--fg-strong);
}
.metric.small .value { font-size: var(--fs-lg); font-weight: 600; }
.metric .label {
    margin-top: 2px;
    color: var(--fg-muted);
    font-size: var(--fs-xs);
    font-weight: 500;
}

/* ==========================================================
   테이블 (key-value · 일반)
   ========================================================== */
table.kv {
    width: 100%;
    font-size: var(--fs-sm);
    border-collapse: collapse;
}
table.kv th {
    text-align: left;
    color: var(--fg-muted);
    font-weight: 500;
    padding: 5px 0;
    width: 40%;
}
table.kv td {
    color: var(--fg);
    padding: 5px 0;
}

table.ttable {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--fs-sm);
}
table.ttable th, table.ttable td {
    text-align: left;
    padding: 7px 10px;
    border-bottom: 1px solid var(--border);
}
table.ttable th {
    color: var(--fg-muted);
    font-weight: 600;
    font-size: var(--fs-xs);
    background: var(--bg-card);
    position: sticky; top: 0;
}
table.ttable tbody tr:nth-child(odd) {
    background: var(--bg-row-alt);
}
table.ttable tbody tr:hover {
    background: var(--bg-card-hover);
}
table.ttable .num { text-align: right; }

/* ==========================================================
   프로세스 카드 - 고정 슬롯 layout shift 0
   ========================================================== */
.card[data-panel="process_top"] {
    min-height: 480px;
}
.card[data-panel="process_top"] .cols {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--sp-4);
    align-items: start;
    flex: 1;
}
@media (max-width: 760px) {
    .card[data-panel="process_top"] { min-height: 820px; }
    .card[data-panel="process_top"] .cols { grid-template-columns: 1fr; }
}
.proc-table {
    table-layout: fixed;
}
.proc-table tbody tr {
    height: 30px;
}
.proc-table tbody tr.placeholder td {
    color: var(--fg-subtle);
    opacity: 0.35;
}
.proc-table tbody td {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    transition: color .2s;
}
.proc-table tbody td:nth-child(2) {
    color: var(--fg);
}

/* ==========================================================
   CPU 코어 바
   ========================================================== */
.cores {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(28px, 1fr));
    gap: 4px;
    margin: var(--sp-2) 0 var(--sp-3);
}
.core-bar {
    height: 36px;
    background: var(--bg-elev);
    border-radius: var(--radius-sm);
    position: relative;
    overflow: hidden;
}
.core-bar > i {
    position: absolute; bottom: 0; left: 0; right: 0;
    background: linear-gradient(0deg, var(--accent), #6fa6ff);
    transition: height .3s ease-out;
}
.core-bar > b {
    position: absolute; bottom: 3px; left: 0; right: 0; text-align: center;
    font-size: 10px; font-weight: 600; color: var(--fg-strong);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
    font-variant-numeric: tabular-nums;
}

/* ==========================================================
   sparkline
   ========================================================== */
.spark-wrap {
    height: 56px;
    margin-top: auto;
    padding-top: var(--sp-2);
}
canvas.spark {
    width: 100%;
    height: 100%;
    display: block;
}

/* ==========================================================
   로그 박스
   ========================================================== */
pre.logbox {
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 12px;
    font-size: var(--fs-xs);
    font-family: ui-monospace, "SF Mono", Consolas, monospace;
    max-height: 160px;
    overflow-y: auto;
    margin: var(--sp-2) 0 0;
    white-space: pre-wrap;
    word-break: break-all;
    color: var(--fg-muted);
    line-height: 1.5;
}

/* ==========================================================
   상태 색 / 태그 / 뱃지
   ========================================================== */
.muted { color: var(--fg-muted); }
.small { font-size: var(--fs-sm); }
.error { color: var(--crit); }

.lvl-ok { color: var(--ok); }
.lvl-warn { color: var(--warn); }
.lvl-critical, .lvl-crit { color: var(--crit); }

.tag {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 2px 8px;
    border-radius: var(--radius-sm);
    font-size: var(--fs-xs);
    font-weight: 500;
    background: var(--bg-elev);
    color: var(--fg-muted);
}
.tag.ok   { background: var(--ok-bg);   color: var(--ok); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.crit { background: var(--crit-bg); color: var(--crit); }

.tag.ok::before   { content: "●"; font-size: 10px; }
.tag.warn::before { content: "▲"; font-size: 10px; }
.tag.crit::before { content: "■"; font-size: 10px; }

/* ==========================================================
   모바일
   ========================================================== */
@media (max-width: 760px) {
    .topbar-inner {
        grid-template-columns: auto 1fr auto;
        gap: var(--sp-2);
        padding: 14px var(--sp-3);
        min-height: 60px;
    }
    .topbar-status {
        gap: var(--sp-2);
        font-size: var(--fs-xs);
    }
    .meta-item .meta-key { display: none; }
    .brand-host { display: none; }
    .page { padding: var(--sp-3); }
    .section-title { margin-top: var(--sp-4); }
    body { font-size: var(--fs-md); }
}

/* ==========================================================
   이벤트 리스트 (커널/SSH 분류 표시)
   ========================================================== */
.sub-title {
    margin: var(--sp-3) 0 var(--sp-2);
    font-size: var(--fs-xs);
    font-weight: 600;
    color: var(--fg-muted);
    letter-spacing: 0.04em;
    text-transform: uppercase;
}
.event-list {
    background: var(--bg-elev);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 8px;
    max-height: 280px;
    overflow-y: auto;
    margin: 6px 0 0;
    font-size: var(--fs-sm);
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.event-empty {
    padding: var(--sp-3);
    text-align: center;
    color: var(--fg-muted);
}
.event-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 8px;
    border-radius: 4px;
    font-variant-numeric: tabular-nums;
    line-height: 1.4;
    min-width: 0;
}
.event-row:hover { background: var(--bg-card-hover); }
.event-tag {
    flex: 0 0 auto;
    min-width: 64px;
    padding: 2px 8px;
    border-radius: 4px;
    background: var(--bg-card);
    color: var(--fg-muted);
    font-size: 11px;
    font-weight: 600;
    text-align: center;
    letter-spacing: 0.02em;
}
.event-time {
    flex: 0 0 auto;
    min-width: 70px;
    color: var(--fg-muted);
    font-size: var(--fs-sm);
}
.event-msg {
    flex: 1 1 auto;
    min-width: 0;
    color: var(--fg);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.event-row.ev-ssh .event-ip {
    flex: 0 0 140px;
    color: var(--fg);
}
.event-row.ev-ssh .event-region {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
}
.event-row.ev-ssh .event-user {
    flex: 0 0 130px;
    color: var(--fg-subtle);
    font-size: 11px;
    text-align: right;
    overflow: hidden;
    text-overflow: ellipsis;
}
.ev-tag-oom            { background: var(--crit-bg); color: var(--crit); }
.ev-tag-panic          { background: var(--crit-bg); color: var(--crit); }
.ev-tag-segfault       { background: var(--warn-bg); color: var(--warn); }
.ev-tag-oops           { background: var(--warn-bg); color: var(--warn); }
.ev-tag-firewall_block { background: var(--info-bg); color: var(--info); }
.ev-tag-trap           { background: var(--warn-bg); color: var(--warn); }
.ev-tag-thermal        { background: var(--warn-bg); color: var(--warn); }
.ev-tag-io_error       { background: var(--warn-bg); color: var(--warn); }
.ev-tag-other          { background: var(--bg-card); color: var(--fg-muted); }


.cc-code {
    display: inline-block;
    padding: 0 5px;
    background: var(--bg-card);
    border-radius: 2px;
    font-size: 10px;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 0.04em;
    margin-right: 2px;
}

/* ==========================================================
   단일 줄 셀 + 표 안정화 (축소 시 레이아웃 보존)
   ========================================================== */
.cell-ellipsis,
table.ttable td.cell-ellipsis {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 0;  /* table-layout fixed의 ellipsis 트릭 */
}

/* 모든 데이터 테이블: 한 줄 강제 + 가로 스크롤 fallback */
table.ttable {
    table-layout: fixed;
    width: 100%;
}
table.ttable th,
table.ttable td {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* systemd 테이블 컬럼 폭 (유닛 | 활성 | 프로그램 | 설명 | 재시작 | 경과) */
table.systemd-table {
    table-layout: fixed;
}
table.systemd-table th:nth-child(1),
table.systemd-table td:nth-child(1) { width: 22%; }
table.systemd-table th:nth-child(2),
table.systemd-table td:nth-child(2) { width: 13%; }
table.systemd-table th:nth-child(3),
table.systemd-table td:nth-child(3) { width: 18%; }
table.systemd-table th:nth-child(4),
table.systemd-table td:nth-child(4) { width: 27%; }
table.systemd-table th:nth-child(5),
table.systemd-table td:nth-child(5) { width: 8%; }
table.systemd-table th:nth-child(6),
table.systemd-table td:nth-child(6) { width: 12%; }

.small { font-size: 11px; }

/* 카드: 좁아져도 내부 요소 깨지지 않도록 최소폭 + 내부 overflow 처리 */
.card {
    min-width: 0;            /* grid 자식 기본 min-width: auto 무력화 */
    overflow: hidden;        /* 카드 외곽 보호 */
}
.card > .row,
.card > table,
.card > .event-list,
.card > .cores,
.card > .spark-wrap {
    min-width: 0;
    max-width: 100%;
}
.metric { min-width: 0; overflow: hidden; }
.metric .value {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}


/* 그리드: 컨테이너 축소 시 카드가 자기 minmax 미만으로 줄지 않게 */
.grid {
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr));
}

/* 좁은 폭에서 systemd 테이블 -- 설명 컬럼 숨김 */
@media (max-width: 1100px) {
    table.systemd-table th:nth-child(4),
    table.systemd-table td:nth-child(4) { display: none; }
    table.systemd-table th:nth-child(1),
    table.systemd-table td:nth-child(1) { width: 30%; }
    table.systemd-table th:nth-child(3),
    table.systemd-table td:nth-child(3) { width: 28%; }
}
@media (max-width: 760px) {
    table.systemd-table th:nth-child(6),
    table.systemd-table td:nth-child(6) { display: none; }
    .event-row.ev-ssh .event-user { display: none; }
    .event-row.ev-ssh .event-ip { flex: 0 0 110px; }
    .event-tag { min-width: 50px; font-size: 10px; }
}

/* ==========================================================
   메트릭 행 -- 카드별 일관된 그리드 정렬
   .row를 flex가 아닌 CSS grid로 변환. 한 카드 내 모든 행이
   동일한 컬럼 수/폭을 가지므로 metric들이 칸 단위로 정렬됨.
   ========================================================== */
.card > .row {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 10px 16px;
    align-items: start;
    margin-bottom: 10px;
}
.card > .row:last-of-type { margin-bottom: 0; }

/* 카드별 컬럼 수 지정 */
[data-panel="cpu"]              > .row { grid-template-columns: repeat(4, minmax(0, 1fr)); }
[data-panel="memory"]           > .row { grid-template-columns: repeat(4, minmax(0, 1fr)); }
[data-panel="swap"]             > .row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
[data-panel="disk"]             > .row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
[data-panel="network"]          > .row { grid-template-columns: repeat(4, minmax(0, 1fr)); }
[data-panel="process_top"]      > .row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
[data-panel="oom_kernel"]       > .row { grid-template-columns: repeat(6, minmax(0, 1fr)); }
[data-panel="systemd_state"]    > .row { grid-template-columns: repeat(4, minmax(0, 1fr)); }
[data-panel="cron_history"]     > .row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
[data-panel="ssh_login_failure"] > .row { grid-template-columns: repeat(4, minmax(0, 1fr)); }

/* metric 자체는 그리드 셀 1개 차지. flex 잔존물 제거 */
.metric {
    flex: unset;
    min-width: 0;
}

/* 좁은 화면 대응 -- 카드별 컬럼 자동 축소 */
@media (max-width: 1100px) {
    [data-panel="cpu"]        > .row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    [data-panel="memory"]     > .row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    [data-panel="oom_kernel"] > .row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    [data-panel="network"]    > .row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 600px) {
    [data-panel="oom_kernel"] > .row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ==========================================================
   접근성: 모션 감소
   ========================================================== */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        transition-duration: 0.01ms !important;
    }
    .core-bar > i { transition: none; }
}
