/* =============================================
   CliniqHub - 메인 스타일
   ============================================= */

/* 다크 테마 (기본) */
:root {
    --bg-primary: #0f1117;
    --bg-secondary: #1a1b23;
    --bg-card: #21222d;
    --bg-hover: #2a2b38;
    --text-primary: #e8e8ed;
    --text-secondary: #9ca3af;
    --text-muted: #6b7280;
    --accent: #6366f1;
    --accent-light: #818cf8;
    --green: #22c55e;
    --green-dim: rgba(34, 197, 94, 0.1);
    --orange: #f59e0b;
    --orange-dim: rgba(245, 158, 11, 0.1);
    --red: #ef4444;
    --red-dim: rgba(239, 68, 68, 0.1);
    --border: rgba(255, 255, 255, 0.08);
    --shadow: rgba(0, 0, 0, 0.3);
    --radius: 12px;
    --radius-sm: 8px;
}

/* 라이트 테마 */
[data-theme="light"] {
    --bg-primary: #f5f5f7;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --bg-hover: #f0f0f3;
    --text-primary: #1a1a2e;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --accent: #6366f1;
    --accent-light: #818cf8;
    --green: #16a34a;
    --green-dim: rgba(22, 163, 74, 0.08);
    --orange: #d97706;
    --orange-dim: rgba(217, 119, 6, 0.08);
    --red: #dc2626;
    --red-dim: rgba(220, 38, 38, 0.08);
    --border: rgba(0, 0, 0, 0.08);
    --shadow: rgba(0, 0, 0, 0.06);
}

[data-theme="light"] .nav {
    box-shadow: 0 1px 3px var(--shadow);
}

[data-theme="light"] .card,
[data-theme="light"] .stat-card {
    box-shadow: 0 1px 3px var(--shadow);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: 'Pretendard', -apple-system, system-ui, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    transition: background 0.3s, color 0.3s;
}

/* 날짜·시간 입력은 브라우저 기본 글꼴(환경에 따라 명조)로 떨어지지 않게 본문 글꼴을 따른다 */
input[type="date"], input[type="time"], input[type="datetime-local"], input[type="month"] { font-family: inherit; }

/* =============================================
   네비게이션
   ============================================= */
.nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    height: 56px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    transition: background 0.3s;
}

.nav-logo {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 18px;
    font-weight: 700;
    color: var(--text-primary);
    text-decoration: none;
}

.nav-logo span { color: var(--accent); }

.nav-links {
    display: flex;
    align-items: center;
    gap: 2px;
}

/* 드롭다운 그룹 */
.nav-group {
    position: relative;
}

.nav-group-label {
    padding: 8px 16px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 6px;
    user-select: none;
    border: none;
    background: none;
}

.nav-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    opacity: 0.6;
    transition: opacity 0.2s;
}

.nav-icon svg {
    width: 100%;
    height: 100%;
}

.nav-group:hover .nav-icon,
.nav-group.has-active .nav-icon {
    opacity: 1;
}

.nav-group-label::after {
    content: '';
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 4px solid currentColor;
    opacity: 0.5;
    transition: transform 0.2s;
}

.nav-group:hover .nav-group-label,
.nav-group-label:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

/* 활성 그룹 (하위 메뉴 중 현재 페이지 포함) */
.nav-group.has-active > .nav-group-label {
    color: var(--accent);
    font-weight: 600;
}

/* 드롭다운 메뉴 */
.nav-dropdown {
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    min-width: 140px;
    padding: 6px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    opacity: 0;
    visibility: hidden;
    transform: translateX(-50%) translateY(4px);
    transition: all 0.15s ease;
    z-index: 1000;
}

.nav-group:hover .nav-dropdown {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(0);
}

.nav-dropdown a {
    display: block;
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.15s;
    white-space: nowrap;
}

.nav-dropdown a:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.nav-dropdown a.active {
    background: var(--accent);
    color: white;
}

/* 기존 단독 링크 (동선관리 등) */
.nav-links > a {
    padding: 8px 16px;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;
}

.nav-links > a:hover { background: var(--bg-hover); color: var(--text-primary); }
.nav-links > a.active { background: var(--accent); color: white; }

.nav-divider {
    width: 1px;
    height: 20px;
    background: var(--border);
    margin: 0 6px;
}

/* =============================================
   네비바 유저 배지
   ============================================= */
.nav-user {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 10px;
    border-radius: var(--radius-sm);
    background: rgba(255,255,255,0.04);
    margin-left: 4px;
}
.nav-user-role {
    font-size: 11px;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 20px;
    letter-spacing: 0.5px;
    line-height: 1.4;
}
.nav-user-role.role-admin { background: rgba(99,102,241,0.2); color: #818cf8; }
.nav-user-role.role-nurse { background: rgba(34,197,94,0.2); color: #22c55e; }
.nav-user-role.role-desk  { background: rgba(245,158,11,0.2); color: #f59e0b; }
.nav-user-name {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: 500;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.nav-logout-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.nav-logout-btn:hover {
    background: var(--red-dim);
    color: var(--red);
}

/* =============================================
   테마 토글 버튼
   ============================================= */
.theme-toggle {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-sm);
    border: none;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    padding: 0;
}

.theme-toggle:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.theme-toggle svg {
    display: block;
}

/* =============================================
   레이아웃
   ============================================= */
.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 24px;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }

/* =============================================
   카드
   ============================================= */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    transition: background 0.3s, border-color 0.3s;
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.card-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.card-subtitle {
    font-size: 13px;
    color: var(--text-muted);
}

/* =============================================
   통계 카드
   ============================================= */
.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    transition: background 0.3s, border-color 0.3s;
}

.stat-label { font-size: 13px; color: var(--text-muted); }
.stat-value { font-size: 28px; font-weight: 700; color: var(--text-primary); }
.stat-value.green { color: var(--green); }
.stat-value.accent { color: var(--accent); }

/* =============================================
   카메라 피드
   ============================================= */
.camera-feed {
    position: relative;
    width: 100%;
    aspect-ratio: 16/9;
    background: #000;
    border-radius: var(--radius);
    overflow: hidden;
}

.camera-feed video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1);
}

.camera-feed canvas {
    position: absolute;
    top: 0; left: 0;
    width: 100%;
    height: 100%;
}

.camera-status {
    position: absolute;
    top: 12px;
    left: 12px;
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(0, 0, 0, 0.6);
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    color: #fff;
}

.camera-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--red);
}

.camera-dot.active {
    background: var(--green);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* =============================================
   Welcome 알림
   ============================================= */
.welcome-toast {
    position: fixed;
    top: 80px;
    right: 24px;
    background: var(--bg-card);
    border: 1px solid var(--green);
    border-radius: var(--radius);
    padding: 20px 24px;
    min-width: 320px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    animation: slideIn 0.4s ease;
    display: none;
}

.welcome-toast.show { display: block; }

.welcome-toast .welcome-icon {
    font-size: 32px;
    margin-bottom: 8px;
}

.welcome-toast .welcome-name {
    font-size: 20px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.welcome-toast .welcome-msg {
    font-size: 14px;
    color: var(--green);
}

.welcome-toast .welcome-time {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 8px;
}

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

@keyframes slideOut {
    from { transform: translateX(0); opacity: 1; }
    to { transform: translateX(100%); opacity: 0; }
}

/* =============================================
   방문 로그
   ============================================= */
.visit-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 400px;
    overflow-y: auto;
}

.visit-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: var(--bg-secondary);
    border-radius: var(--radius-sm);
}

.visit-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    background: var(--bg-hover);
}

.visit-info {
    flex: 1;
    min-width: 0;
}

.visit-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
}

.visit-time {
    font-size: 12px;
    color: var(--text-muted);
}

.visit-badge {
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 11px;
    font-weight: 600;
}

.visit-badge.high { background: var(--green-dim); color: var(--green); }
.visit-badge.medium { background: var(--orange-dim); color: var(--orange); }

/* =============================================
   폼
   ============================================= */
.form-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 16px;
}

.form-label {
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}

.form-input {
    padding: 10px 14px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 14px;
    outline: none;
    transition: border 0.2s, background 0.3s;
}

.form-input:focus { border-color: var(--accent); }

textarea.form-input {
    resize: vertical;
    min-height: 80px;
}

/* =============================================
   버튼
   ============================================= */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    border: none;
    transition: all 0.2s;
}

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-light); }

.btn-secondary { background: var(--bg-hover); color: var(--text-primary); }
.btn-secondary:hover { background: var(--border); }

.btn-danger { background: var(--red-dim); color: var(--red); }
.btn-danger:hover { background: rgba(239, 68, 68, 0.2); }

.btn-sm { padding: 6px 12px; font-size: 12px; }

/* =============================================
   테이블
   ============================================= */
.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th {
    text-align: left;
    padding: 10px 12px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid var(--border);
}

td {
    padding: 12px;
    font-size: 14px;
    border-bottom: 1px solid var(--border);
}

tr:hover td { background: var(--bg-hover); }

/* =============================================
   사진 캡처 영역
   ============================================= */
.capture-area {
    position: relative;
    width: 100%;
    max-width: 400px;
    aspect-ratio: 1;
    background: #000;
    border-radius: var(--radius);
    overflow: hidden;
    margin: 0 auto;
}

.capture-area video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1);
}

.capture-area canvas {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.capture-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 200px;
    height: 250px;
    border: 2px dashed rgba(255, 255, 255, 0.3);
    border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
}

.capture-actions {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-top: 16px;
}

/* =============================================
   Empty state
   ============================================= */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    color: var(--text-muted);
    font-size: 14px;
    gap: 8px;
}

.empty-state .emoji { font-size: 32px; }

/* =============================================
   스크롤바
   ============================================= */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* =============================================
   모바일 햄버거 메뉴 (데스크톱 숨김)
   ============================================= */
.nav-hamburger {
    display: none;
    width: 44px;
    height: 44px;
    border: none;
    background: none;
    color: var(--text-primary);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    padding: 0;
    border-radius: var(--radius-sm);
    transition: background 0.2s;
}

.nav-hamburger:hover {
    background: var(--bg-hover);
}

.nav-hamburger svg {
    width: 22px;
    height: 22px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
}

/* =============================================
   모바일 반응형 (768px 이하)
   ============================================= */
@media (max-width: 768px) {
    /* --- 네비게이션 --- */
    .nav {
        padding: 0 12px;
        height: 52px;
    }

    .nav-hamburger {
        display: inline-flex;
    }

    .nav-divider {
        display: none;
    }

    .nav-links {
        position: fixed;
        top: 52px;
        left: 0;
        right: 0;
        bottom: 0;
        height: calc(100vh - 52px);
        background: var(--bg-secondary);
        flex-direction: column;
        align-items: stretch;
        padding: 12px 16px;
        overflow-y: auto;
        z-index: 999;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        gap: 4px;
        border-top: 1px solid var(--border);
    }

    .nav-links.open {
        transform: translateX(0);
    }

    /* 모바일: 그룹 레이블 풀 폭 */
    .nav-group {
        position: static;
    }

    .nav-group-label {
        width: 100%;
        padding: 12px 16px;
        font-size: 15px;
    }

    .nav-icon {
        width: 20px;
        height: 20px;
        opacity: 0.5;
    }

    .nav-group.has-active .nav-icon,
    .nav-group.expanded .nav-icon {
        opacity: 0.9;
    }

    .nav-group-label::after {
        transition: transform 0.2s;
        margin-left: auto;
    }

    .nav-group.expanded .nav-group-label::after {
        transform: rotate(180deg);
    }

    /* 모바일: 드롭다운 인라인 아코디언 */
    .nav-dropdown {
        position: static !important;
        transform: none !important;
        opacity: 1 !important;
        visibility: visible !important;
        box-shadow: none;
        border: none;
        background: transparent;
        padding: 0 0 0 16px;
        min-width: 0;
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s ease;
    }

    .nav-group.expanded .nav-dropdown {
        max-height: 300px;
    }

    .nav-dropdown a {
        padding: 10px 16px;
        font-size: 14px;
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    /* 유저 배지 모바일 */
    .nav-user {
        width: 100%;
        padding: 10px 16px;
        margin: 8px 0 0;
        background: rgba(255,255,255,0.03);
        border-radius: var(--radius-sm);
    }

    /* 테마 토글 */
    .theme-toggle {
        width: 44px;
        height: 44px;
        align-self: flex-start;
        margin-top: 8px;
    }

    /* --- 레이아웃 --- */
    .container {
        padding: 16px 12px;
    }

    .grid-2 {
        grid-template-columns: 1fr;
        gap: 16px;
    }

    .grid-3 {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    /* --- 카드 --- */
    .card {
        padding: 14px;
    }

    .card-header {
        margin-bottom: 12px;
        flex-wrap: wrap;
        gap: 8px;
    }

    .card-title {
        font-size: 15px;
    }

    /* --- 통계 카드 --- */
    .stat-card {
        padding: 14px;
    }

    .stat-value {
        font-size: 22px;
    }

    /* --- 카메라 피드 --- */
    .camera-feed {
        aspect-ratio: 4/3;
    }

    /* --- 방문 로그 --- */
    .visit-list {
        max-height: 300px;
    }

    .visit-avatar {
        width: 36px;
        height: 36px;
    }

    /* --- 폼 --- */
    .form-input {
        padding: 12px 14px;
        font-size: 16px; /* iOS 자동줌 방지 */
    }

    /* --- 버튼 --- */
    .btn {
        min-height: 44px;
        padding: 10px 16px;
    }

    .btn-sm {
        min-height: 36px;
        padding: 8px 12px;
    }

    /* --- 테이블 --- */
    .table-wrap {
        -webkit-overflow-scrolling: touch;
    }

    th {
        padding: 8px;
        font-size: 11px;
    }

    td {
        padding: 8px;
        font-size: 13px;
    }

    /* --- 캡처 영역 --- */
    .capture-area {
        max-width: 100%;
    }

    .capture-overlay {
        width: 160px;
        height: 200px;
    }

    .capture-actions {
        flex-wrap: wrap;
    }

    /* --- Welcome 토스트 --- */
    .welcome-toast {
        right: 12px;
        left: 12px;
        min-width: auto;
        top: 64px;
        padding: 16px;
    }

    /* --- 빈 상태 --- */
    .empty-state {
        padding: 24px;
    }
}

/* =============================================
   초소형 모바일 (480px 이하)
   ============================================= */
@media (max-width: 480px) {
    .container {
        padding: 12px 8px;
    }

    .stat-value {
        font-size: 20px;
    }

    .card {
        padding: 12px;
        border-radius: var(--radius-sm);
    }
}

/* =============================================
   10-29 모바일·태블릿 보정 (데스크톱 1024px 이상은 영향 없음)
   ============================================= */

/* 태블릿 세로 (769~1023px): 상단 메뉴가 두 줄로 쪼개지지 않게 압축 */
@media screen and (min-width: 769px) and (max-width: 1023px) {
    .nav { padding: 0 16px; gap: 8px; }
    .nav-logo { gap: 0; flex-shrink: 0; min-height: 40px; }
    .nav-links { gap: 0; min-width: 0; }
    .nav-group-label { padding: 8px 10px; gap: 5px; min-height: 40px; white-space: nowrap; }
    .nav-icon { display: none; }
    .nav-divider { margin: 0 4px; }
    .nav-user { gap: 4px; padding: 2px 4px 2px 8px; flex-shrink: 0; }
    .nav-user-role { white-space: nowrap; }
    .nav-user-name { max-width: 88px; }
    .nav-logout-btn { width: 40px; height: 40px; }
    .theme-toggle { width: 40px; height: 40px; }
    .nav-dropdown a { padding: 11px 14px; }
}

/* 휴대폰·태블릿 공통: 한국어 단어 단위 줄바꿈 */
@media screen and (max-width: 1023px) {
    body { word-break: keep-all; overflow-wrap: anywhere; }
}

/* 휴대폰 (768px 이하) */
@media screen and (max-width: 768px) {
    .nav {
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(4px, env(safe-area-inset-right));
    }
    .nav-logo { gap: 0; min-height: 44px; }

    /* 메뉴 패널: 노치·홈 바 여백, 뒤 화면 스크롤 막기 */
    .nav-links {
        height: auto;
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
        padding-bottom: calc(24px + env(safe-area-inset-bottom));
        overscroll-behavior: contain;
    }
    html.nav-menu-open,
    html.nav-menu-open body { overflow: hidden; }

    .nav-group-label { min-height: 48px; white-space: nowrap; }
    .nav-dropdown a { min-height: 46px; }

    /* 사용자 영역: 이름 전체 표시, 아이콘 버튼 44px */
    .nav-user { gap: 6px; padding: 6px 8px 6px 16px; }
    .nav-user-role { white-space: nowrap; }
    .nav-user-name { flex: 1; min-width: 0; max-width: none; font-size: 14px; }
    .nav-logout-btn { width: 44px; height: 44px; flex-shrink: 0; }

    /* 공통 폼: 아이폰 입력 확대 방지 (페이지별 스타일이 우선) */
    input:where(:not([type="checkbox"], [type="radio"], [type="range"], [type="file"], [type="color"], [type="button"], [type="submit"], [type="reset"])),
    select,
    textarea { font-size: 16px; }
    .btn-sm { min-height: 40px; }

    /* 공통 표: 넘치면 표 상자 안에서만 가로 스크롤 */
    .table-wrap { max-width: 100%; overscroll-behavior-x: contain; }

    .welcome-toast { top: calc(64px + env(safe-area-inset-top)); }
}
