  :root {
    --primary: #2196F3;
    --primary-dark: #1565C0;
    --primary-light: #E3F2FD;
    --success: #4CAF50;
    --warning: #FF9800;
    --danger: #F44336;
    --gray-100: #f5f6fa;
    --gray-200: #e8eaf0;
    --gray-500: #9E9E9E;
    --gray-700: #616161;
    --gray-900: #212121;
    --sidebar-w: 250px;
    --header-h: 60px;
  }
  * { margin:0; padding:0; box-sizing:border-box; }
  body { font-family: 'Segoe UI', Tahoma, sans-serif; background: var(--gray-100); color: var(--gray-900); display:flex; min-height:100vh; }

  /* SIDEBAR */
  .sidebar {
    width: var(--sidebar-w);
    background: #fff;
    box-shadow: 2px 0 8px rgba(0,0,0,0.08);
    display: flex; flex-direction: column;
    position: fixed; left:0; top:0; bottom:0; z-index:100;
    transition: transform 0.3s;
    overflow-y: auto;
  }
  .sidebar-logo {
    padding: 20px 16px;
    border-bottom: 1px solid var(--gray-200);
    display: flex; flex-direction: column; align-items: center; gap: 8px;
  }
  .logo-icon {
    width: 56px; height: 56px;
    background: var(--primary);
    border-radius: 12px;
    display: flex; align-items: center; justify-content: center;
    color: #fff; font-size: 22px; font-weight: 700;
  }
  .school-name { font-size: 14px; font-weight: 700; color: var(--gray-900); text-align:center; }
  .school-sub { font-size: 11px; color: var(--gray-500); }

  .nav-section-label {
    font-size: 10px; font-weight: 700; color: var(--gray-500);
    text-transform: uppercase; letter-spacing: 1px;
    padding: 16px 16px 4px;
  }
  .nav-item {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 16px; cursor: pointer;
    border-radius: 8px; margin: 2px 8px;
    font-size: 13.5px; color: var(--gray-700);
    transition: all 0.15s; white-space: nowrap;
    text-decoration: none;
  }
  .nav-item:hover { background: var(--primary-light); color: var(--primary); }
  .nav-item.active { background: var(--primary); color: #fff; font-weight: 600; }
  .nav-item .nav-icon { font-size: 18px; width: 22px; text-align: center; flex-shrink:0; }
  .nav-badge {
    margin-left: auto; background: var(--danger);
    color: #fff; font-size: 10px; font-weight: 700;
    padding: 1px 6px; border-radius: 20px;
  }
  .sidebar-footer { padding: 12px 8px; border-top: 1px solid var(--gray-200); margin-top: auto; }

  /* MAIN */
  .main { margin-left: var(--sidebar-w); flex:1; display:flex; flex-direction:column; min-height:100vh; }
  .topbar {
    height: var(--header-h); background: #fff;
    box-shadow: 0 2px 6px rgba(0,0,0,0.06);
    display: flex; align-items: center; padding: 0 24px;
    position: sticky; top: 0; z-index: 50; gap: 12px;
  }
  .menu-toggle { display:none; font-size:20px; cursor:pointer; color: var(--gray-700); background:none; border:none; }
  .page-title { font-size: 17px; font-weight: 700; color: var(--gray-900); }
  .topbar-right { margin-left: auto; display:flex; align-items:center; gap: 12px; }
  .topbar-btn {
    width: 36px; height: 36px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    background: var(--gray-100); border: none; cursor: pointer; font-size: 18px; position: relative;
  }
  .notif-dot { position:absolute; top:4px; right:4px; width:8px; height:8px; background:var(--danger); border-radius:50%; }
  .avatar { width:36px; height:36px; border-radius:50%; background:var(--primary); color:#fff; display:flex; align-items:center; justify-content:center; font-weight:700; font-size:14px; cursor:pointer; }

  /* CONTENT */
  .content { padding: 24px; flex:1; }

  /* CARDS */
  .stat-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; margin-bottom: 24px; }
  .stat-card {
    background: #fff; border-radius: 12px; padding: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    display: flex; align-items: center; gap: 16px;
    transition: transform 0.15s, box-shadow 0.15s;
  }
  .stat-card:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0,0,0,0.12); }
  .stat-icon { width:52px; height:52px; border-radius:12px; display:flex; align-items:center; justify-content:center; font-size:24px; flex-shrink:0; }
  .stat-icon.blue { background: #E3F2FD; }
  .stat-icon.green { background: #E8F5E9; }
  .stat-icon.orange { background: #FFF3E0; }
  .stat-icon.purple { background: #F3E5F5; }
  .stat-icon.red { background: #FFEBEE; }
  .stat-num { font-size: 26px; font-weight: 800; color: var(--gray-900); line-height:1; }
  .stat-label { font-size: 12px; color: var(--gray-500); margin-top: 4px; }

  /* GRID */
  .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 24px; }
  .grid-3 { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; margin-bottom: 24px; }
  .card { background: #fff; border-radius: 12px; padding: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
  .card-header { display:flex; align-items:center; justify-content:space-between; margin-bottom:16px; }
  .card-title { font-size: 15px; font-weight: 700; }

  /* TABLE */
  .table-wrap { overflow-x: auto; }
  table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
  th { background: var(--gray-100); padding: 10px 14px; text-align: left; font-size: 12px; font-weight: 700; color: var(--gray-700); white-space: nowrap; }
  td { padding: 12px 14px; border-bottom: 1px solid var(--gray-200); color: var(--gray-900); }
  tr:last-child td { border-bottom: none; }
  tr:hover td { background: var(--gray-100); }
  .badge { display:inline-flex; align-items:center; padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 700; }
  .badge-success { background: #E8F5E9; color: #2E7D32; }
  .badge-warning { background: #FFF3E0; color: #E65100; }
  .badge-danger { background: #FFEBEE; color: #B71C1C; }
  .badge-info { background: #E3F2FD; color: #1565C0; }
  .badge-gray { background: #F5F5F5; color: #616161; }

  /* BUTTONS */
  .btn { display:inline-flex; align-items:center; gap:6px; padding: 8px 18px; border-radius: 8px; border:none; cursor:pointer; font-size:13.5px; font-weight:600; transition: all 0.15s; }
  .btn-primary { background: var(--primary); color: #fff; }
  .btn-primary:hover { background: var(--primary-dark); }
  .btn-outline { background: #fff; color: var(--primary); border: 1.5px solid var(--primary); }
  .btn-outline:hover { background: var(--primary-light); }
  .btn-danger { background: var(--danger); color: #fff; }
  .btn-sm { padding: 5px 12px; font-size: 12px; }
  .btn-icon { width:32px; height:32px; padding:0; justify-content:center; background: var(--gray-100); color: var(--gray-700); border-radius:8px; }
  .btn-icon:hover { background: var(--gray-200); }

  /* FORM */
  .form-group { margin-bottom: 16px; }
  label { display:block; font-size: 12px; font-weight: 700; color: var(--gray-700); margin-bottom: 6px; }
  input, select, textarea {
    width: 100%; padding: 9px 12px; border: 1.5px solid var(--gray-200);
    border-radius: 8px; font-size: 13.5px; font-family: inherit;
    transition: border-color 0.15s; outline: none;
  }
  input:focus, select:focus, textarea:focus { border-color: var(--primary); }
  .form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
  .search-bar { display:flex; align-items:center; gap:10px; margin-bottom:16px; flex-wrap:wrap; }
  .search-input-wrap { position:relative; flex:1; min-width:180px; }
  .search-input-wrap input { padding-left: 36px; }
  .search-icon { position:absolute; left:10px; top:50%; transform:translateY(-50%); color:var(--gray-500); font-size:16px; pointer-events:none; }

  /* MODAL */
  .modal-overlay {
    position: fixed; inset:0; background: rgba(0,0,0,0.45);
    display: flex; align-items: center; justify-content: center;
    z-index: 200; padding: 16px;
    opacity:0; pointer-events:none; transition: opacity 0.2s;
  }
  .modal-overlay.open { opacity:1; pointer-events:all; }
  .modal {
    background: #fff; border-radius: 16px; width: 100%; max-width: 520px;
    max-height: 90vh; overflow-y: auto;
    transform: scale(0.95); transition: transform 0.2s;
  }
  .modal-overlay.open .modal { transform: scale(1); }
  .modal-header { padding: 20px 24px 16px; display:flex; align-items:center; justify-content:space-between; border-bottom: 1px solid var(--gray-200); }
  .modal-title { font-size: 16px; font-weight: 700; }
  .modal-close { background:none; border:none; font-size:22px; cursor:pointer; color:var(--gray-500); line-height:1; }
  .modal-body { padding: 20px 24px; }
  .modal-footer { padding: 16px 24px; border-top: 1px solid var(--gray-200); display:flex; gap:10px; justify-content:flex-end; }

  /* SCHEDULE */
  .schedule-grid { display: grid; grid-template-columns: 60px repeat(7, 1fr); gap: 2px; font-size: 12px; }
  .sch-header { background: var(--primary); color: #fff; padding: 8px 4px; text-align: center; border-radius: 4px; font-weight: 700; }
  .sch-time { background: var(--gray-100); padding: 8px 4px; text-align: center; color: var(--gray-700); border-radius: 4px; font-size: 11px; }
  .sch-cell { background: var(--gray-100); min-height: 40px; border-radius: 4px; display: flex; flex-direction: column; gap: 2px; padding: 2px; }
  .sch-event {
    border-radius: 4px;
    padding: 4px 6px; font-size: 11px; font-weight: 600; cursor: pointer;
    overflow: hidden; line-height: 1.3;
  }

  /* ATTENDANCE */
  .attend-student { display:flex; align-items:center; gap:10px; padding: 10px 0; border-bottom: 1px solid var(--gray-200); }
  .attend-student:last-child { border-bottom: none; }
  .attend-avatar { width:36px; height:36px; border-radius:50%; background:var(--primary-light); color:var(--primary); display:flex; align-items:center; justify-content:center; font-weight:700; flex-shrink:0; overflow:hidden; }
  .attend-avatar img { width:100%; height:100%; object-fit:cover; border-radius:50%; }
  .stu-photo-btn { background:none;border:1.5px dashed var(--gray-300);border-radius:50%;cursor:pointer;display:flex;align-items:center;justify-content:center;color:var(--gray-500);font-size:11px;flex-shrink:0;overflow:hidden; }
  .attend-name { flex:1; font-size: 13.5px; font-weight: 600; }
  .attend-status { display:flex; gap:6px; }
  .status-btn { padding: 5px 12px; border: 1.5px solid var(--gray-200); border-radius: 20px; font-size: 11px; font-weight: 700; cursor:pointer; background: #fff; }
  .status-btn.present { border-color: var(--success); background: #E8F5E9; color: #2E7D32; }
  .status-btn.absent { border-color: var(--danger); background: #FFEBEE; color: #B71C1C; }
  .status-btn.leave { border-color: var(--warning); background: #FFF3E0; color: #E65100; }

  /* TABS */
  .tabs { display:flex; gap:4px; margin-bottom:20px; border-bottom: 2px solid var(--gray-200); }
  .tab-btn { padding: 10px 18px; border:none; background:none; cursor:pointer; font-size:13.5px; font-weight:600; color:var(--gray-500); border-bottom: 3px solid transparent; margin-bottom: -2px; }
  .tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }

  /* SIDEBAR OVERLAY for mobile */
  .sidebar-overlay { display:none; position:fixed; inset:0; background:rgba(0,0,0,0.4); z-index:99; }

  @media print {
    .sidebar, .topbar, .sidebar-overlay, .toast, .modal-overlay, .pin-screen, .loading-overlay { display:none!important; }
    .main { margin-left:0!important; }
    .content { padding:8px!important; }
    .btn { display:none!important; }
    table { font-size:12px; }
    th, td { padding:6px 10px; }
    .no-print { display:none!important; }
    .print-only { display:block!important; }
  }
  .print-only { display:none; }

  /* EMPTY STATE */
  .empty { text-align:center; padding: 40px 20px; color: var(--gray-500); }
  .empty-icon { font-size: 48px; margin-bottom: 12px; }

  /* MONTH PICKER */
  .month-select { padding: 6px 10px; border: 1.5px solid var(--gray-200); border-radius: 8px; font-size: 13px; cursor: pointer; }

  /* PAYMENT STATUS */
  .pay-row { display:flex; align-items:center; gap:12px; padding:12px 0; border-bottom:1px solid var(--gray-200); }
  .pay-row:last-child { border-bottom:none; }
  .pay-info { flex:1; }
  .pay-name { font-size: 13.5px; font-weight: 700; }
  .pay-course { font-size: 12px; color: var(--gray-500); }
  .pay-amount { font-size: 15px; font-weight: 800; color: var(--primary); white-space: nowrap; }

  /* Progress bar */
  .progress { background: var(--gray-200); border-radius: 20px; height: 8px; overflow:hidden; }
  .progress-bar { height:100%; border-radius:20px; background: var(--primary); }

  /* Notification toast */
  .toast {
    position: fixed; bottom: 24px; right: 24px;
    background: #333; color: #fff; padding: 12px 20px;
    border-radius: 10px; font-size: 13.5px; z-index: 999;
    opacity: 0; transform: translateY(20px);
    transition: all 0.3s; pointer-events:none;
    max-width: 300px;
  }
  .toast.show { opacity:1; transform: translateY(0); }
  .toast.success { background: var(--success); }
  /* ซ่อนปุ่มแก้ไข/ลบสำหรับผู้ช่วย */
  .assistant-mode .owner-only { display:none!important; }

  /* LOADING OVERLAY */
  .loading-overlay {
    position: fixed; inset: 0; background: rgba(255,255,255,0.95);
    z-index: 9999; display: flex; flex-direction: column;
    align-items: center; justify-content: center; gap: 16px;
  }
  .loading-overlay.hidden { display: none; }
  .spinner {
    width: 40px; height: 40px; border: 4px solid var(--gray-200);
    border-top-color: var(--primary); border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  /* PIN SCREEN */
  .pin-screen {
    position: fixed; inset: 0; background: linear-gradient(135deg, #1565C0 0%, #2196F3 100%);
    z-index: 9999; display: flex; align-items: center; justify-content: center; padding: 20px;
  }
  .pin-screen.hidden { display: none; }
  .pin-card {
    background: #fff; border-radius: 24px; padding: 40px 36px;
    max-width: 360px; width: 100%; box-shadow: 0 20px 60px rgba(0,0,0,0.3); text-align: center;
  }
  .pin-logo { font-size: 48px; margin-bottom: 8px; }
  .pin-title { font-size: 20px; font-weight: 800; color: var(--gray-900); margin-bottom: 4px; }
  .pin-sub { font-size: 13px; color: var(--gray-500); margin-bottom: 28px; }
  .pin-dots { display: flex; gap: 12px; justify-content: center; margin-bottom: 28px; }
  .pin-dot {
    width: 18px; height: 18px; border-radius: 50%;
    border: 2px solid var(--gray-200); background: #fff; transition: all 0.15s;
  }
  .pin-dot.filled { background: var(--primary); border-color: var(--primary); transform: scale(1.1); }
  .pin-dot.error { background: var(--danger); border-color: var(--danger); }
  .pin-keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
  .pin-key {
    height: 60px; border-radius: 14px; border: 1.5px solid var(--gray-200);
    background: #fff; font-size: 22px; font-weight: 700; color: var(--gray-900);
    cursor: pointer; transition: all 0.1s; display: flex; align-items: center; justify-content: center;
  }
  .pin-key:hover { background: var(--primary-light); border-color: var(--primary); }
  .pin-key:active { transform: scale(0.94); background: var(--primary); color: #fff; }
  .pin-key.del { font-size: 18px; color: var(--gray-500); }
  .pin-error { color: var(--danger); font-size: 13px; font-weight: 600; margin-top: 16px; min-height: 20px; }

  /* SETUP SCREEN */
  .setup-screen {
    position: fixed; inset: 0; background: var(--gray-100);
    z-index: 9998; display: flex; align-items: center; justify-content: center; padding: 20px;
  }
  .setup-screen.hidden { display: none; }
  .setup-card {
    background: #fff; border-radius: 20px; padding: 36px;
    max-width: 520px; width: 100%; box-shadow: 0 8px 32px rgba(0,0,0,0.12);
  }
  .setup-step { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 16px; }
  .step-num { width: 28px; height: 28px; background: var(--primary); color: #fff; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 13px; flex-shrink: 0; margin-top: 2px; }

  /* SYNC indicator */
  .sync-dot {
    width: 8px; height: 8px; border-radius: 50%; background: var(--success);
    display: inline-block; margin-right: 6px;
    box-shadow: 0 0 0 0 rgba(76,175,80,0.4);
    animation: pulse-green 2s infinite;
  }
  .sync-dot.offline { background: var(--danger); animation: none; }
  @keyframes pulse-green {
    0% { box-shadow: 0 0 0 0 rgba(76,175,80,0.4); }
    70% { box-shadow: 0 0 0 8px rgba(76,175,80,0); }
    100% { box-shadow: 0 0 0 0 rgba(76,175,80,0); }
  }

  /* BOTTOM NAV (mobile only) */
  .bottom-nav {
    display: none;
    position: fixed; bottom: 0; left: 0; right: 0;
    height: 62px; background: #fff;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.10);
    z-index: 150;
    flex-direction: row; align-items: stretch;
  }
  .bnav-item {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center; gap: 3px;
    cursor: pointer; border: none; background: none;
    color: var(--gray-500); font-size: 10px; font-weight: 600;
    padding: 6px 0 8px; transition: color 0.15s;
    -webkit-tap-highlight-color: transparent;
  }
  .bnav-item .bnav-icon { font-size: 22px; line-height: 1; }
  .bnav-item.active { color: var(--primary); }
  .bnav-more-menu {
    display: none; position: fixed; bottom: 62px; left: 0; right: 0;
    background: #fff; box-shadow: 0 -2px 16px rgba(0,0,0,0.12);
    z-index: 149; padding: 12px 8px; flex-direction: column; gap: 4px;
    max-height: 60vh; overflow-y: auto;
  }
  .bnav-more-menu.open { display: flex; }
  .bnav-more-item {
    display: flex; align-items: center; gap: 14px;
    padding: 12px 16px; border-radius: 10px; cursor: pointer;
    font-size: 14px; font-weight: 600; color: var(--gray-900);
    border: none; background: none; text-align: left;
    -webkit-tap-highlight-color: transparent;
  }
  .bnav-more-item:active { background: var(--gray-100); }
  .bnav-more-item .nav-icon { font-size: 20px; width: 28px; text-align: center; }

  /* Responsive */
  @media (max-width: 768px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); }
    .sidebar-overlay.open { display:block; }
    .main { margin-left: 0; }
    .menu-toggle { display:none; }
    .grid-2, .grid-3 { grid-template-columns: 1fr; }
    .stat-cards { grid-template-columns: 1fr 1fr; }
    .form-row { grid-template-columns: 1fr; }
    .schedule-grid { grid-template-columns: 50px repeat(7,1fr); font-size:10px; }
    .content { padding: 14px 12px 80px; }
    .topbar { padding: 0 14px; }
    body { overflow-x: hidden; }
    .main { overflow-x: hidden; max-width: 100vw; }
    .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
    table { min-width: 480px; }
    .card { padding: 14px 12px; }
    .bottom-nav { display: flex; }
    /* Enrollment cards full width */
    .enroll-card { width: 100% !important; }
    /* stat cards compact */
    .stat-card { padding: 14px 12px; gap: 10px; }
    .stat-num { font-size: 22px; }
    .stat-icon { width: 44px; height: 44px; font-size: 20px; }
    /* modal full width on mobile */
    .modal { max-width: 100%; border-radius: 16px 16px 0 0; align-self: flex-end; margin-top: auto; max-height: 92vh; }
    .modal-overlay { align-items: flex-end; padding: 0; }
    /* topbar shrink */
    .page-title { font-size: 15px; }
    #syncStatus span:last-child { display: none; }
  }
  @media (max-width: 480px) {
    .stat-cards { grid-template-columns: 1fr 1fr; }
    .attend-status { flex-wrap: wrap; gap: 6px; }
    .tabs { flex-wrap: wrap; gap: 2px; }
    .tab-btn { padding: 8px 12px; font-size: 12.5px; }
    .status-btn { padding: 7px 14px; font-size: 12px; }
    .btn { padding: 9px 14px; font-size: 13px; }
    table { min-width: 380px; }
  }

  /* ===== v2 additions ===== */
  .sync-dot.saving { background: var(--warning); animation: none; }
  .login-form { text-align: left; margin-top: 4px; }
  .login-form .form-group { margin-bottom: 14px; }
  .login-form input { width: 100%; }
  .login-btn { width: 100%; justify-content: center; padding: 12px; font-size: 15px; }
  .login-link { background: none; border: none; color: var(--gray-500); cursor: pointer; font-size: 12px; text-decoration: underline; margin-top: 12px; }
  .settings-section { margin-bottom: 18px; }
  .settings-section .card-title { display: flex; align-items: center; gap: 8px; }
  .settings-hint { font-size: 12.5px; color: var(--gray-500); margin-bottom: 12px; line-height: 1.6; }
  .settings-actions { display: flex; gap: 8px; flex-wrap: wrap; }
  .audit-row td { font-size: 12.5px; vertical-align: top; }
  .audit-change { color: var(--gray-500); font-size: 11.5px; }
  .import-report { background: var(--gray-100); border-radius: 10px; padding: 12px 14px; font-size: 13px; line-height: 1.8; }
  .hidden { display: none !important; }
