:root {
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --ink: #172033;
  --muted: #667085;
  --line: #e7edf5;
  --soft: #f5f8fc;
  --white: #fff;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--soft);
  color: var(--ink);
  font-family: "Inter", "Noto Sans Thai", system-ui, sans-serif;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

html { width: 100%; overflow-x: hidden; }
img, svg, video, canvas { max-width: 100%; height: auto; }

.app-loader {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f8fbff;
  color: var(--ink);
  transition: opacity .35s ease, visibility .35s ease;
}
.app-loader.is-hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.loader-box { text-align: center; }
.loader-mark {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  border-radius: 18px;
  background: var(--blue);
  color: #fff;
  font-weight: 800;
  box-shadow: 0 18px 40px rgba(37, 99, 235, .25);
}
.app-loader .loader-mark,
.app-loader .loader-icon,
.app-loader .app-loader-logo {
  display: none !important;
}
.loader-title {
  margin: 0 0 8px;
  font-size: 24px;
  font-weight: 800;
  color: #0f172a;
  letter-spacing: -0.02em;
}
.loader-text {
  margin: 0;
  font-size: 15px;
  color: #64748b;
}

.page-fade { animation: fadeIn .35s ease both; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.app-shell { min-height: 100vh; padding-left: 272px; }
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 272px;
  background: #fff;
  border-right: 1px solid var(--line);
  padding: 22px;
  z-index: 1000;
}
.brand { display: flex; gap: 12px; align-items: center; color: var(--ink); text-decoration: none; margin-bottom: 28px; }
.brand span { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; background: var(--blue); color: white; font-weight: 800; }
.brand strong { line-height: 1.15; }
.brand small { display: block; color: var(--muted); font-weight: 600; font-size: .76rem; margin-top: 2px; }
.section-title { font-size: 1rem; font-weight: 800; margin: 8px 0 0; }
.sidebar nav a {
  display: block;
  padding: 12px 14px;
  margin: 4px 0;
  color: #475467;
  border-radius: 8px;
  text-decoration: none;
  transition: .2s ease;
}
.sidebar nav a:hover, .sidebar nav a.active { background: #eef5ff; color: var(--blue-dark); transform: translateX(2px); }

.topbar {
  min-height: 78px;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 28px;
  background: rgba(255,255,255,.86);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 900;
}
.topbar h1 { margin: 0; font-size: 1.35rem; font-weight: 800; }
.app-main { min-height: calc(100vh - 78px); }
.topbar-brand, .profile-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: var(--blue);
  color: #fff;
  border: 0;
  text-decoration: none;
  font-weight: 800;
}
.profile-btn { background: #eef5ff; color: var(--blue-dark); }
.topbar-title { min-width: 0; }

.panel, .stat-card, .login-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 14px 34px rgba(16, 24, 40, .04);
}
.panel { padding: 22px; }
.panel h2 { font-size: 1.05rem; font-weight: 800; margin: 0 0 16px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.panel-head h2 { margin: 0; }

.stat-card {
  min-height: 126px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: transform .2s ease, box-shadow .2s ease;
}
.stat-card:hover, .panel:hover { transform: translateY(-2px); box-shadow: 0 18px 42px rgba(16, 24, 40, .07); }
.stat-card span, .mini-grid span, .list-line small { color: var(--muted); font-size: .86rem; }
.stat-card strong { font-size: clamp(1.35rem, 2vw, 1.85rem); }

.btn { border-radius: 8px; transition: transform .18s ease, box-shadow .18s ease; }
.btn, .form-control, .form-select { min-height: 44px; font-size: 16px; }
.form-check-input { min-width: 20px; min-height: 20px; }
.btn:hover { transform: translateY(-1px); }
.btn-primary { background: var(--blue); border-color: var(--blue); }
.btn-primary:hover { background: var(--blue-dark); border-color: var(--blue-dark); }
.form-control, .form-select { border-radius: 8px; border-color: #d8e1ed; }

.list-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.list-line:last-child { border-bottom: 0; }
.list-line small { display: block; margin-top: 3px; }
.mini-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.mini-grid > div { background: var(--soft); border-radius: 8px; padding: 14px; }
.action-cell { white-space: nowrap; }
.mobile-card-list { display: none; }
.empty-state { padding: 18px; text-align: center; color: var(--muted); background: var(--soft); border-radius: 8px; }
.mobile-record-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  box-shadow: 0 10px 26px rgba(16, 24, 40, .04);
}
.record-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.record-head h3 { margin: 0; font-size: 1rem; font-weight: 800; overflow-wrap: anywhere; }
.record-head p { margin: 3px 0 0; color: var(--muted); font-size: .88rem; }
.record-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 12px; }
.record-grid div, .report-row { background: var(--soft); border-radius: 8px; padding: 10px; min-width: 0; }
.record-grid span, .report-row span { color: var(--muted); display: block; font-size: .8rem; }
.record-grid strong, .report-row strong { display: block; margin-top: 2px; font-size: .92rem; overflow-wrap: anywhere; }
.record-actions { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 12px; }
.record-actions .btn, .record-actions form, .record-actions button { width: 100%; }
.record-amount { white-space: nowrap; color: var(--blue-dark); }
.slip-link { display: inline-block; margin-top: 10px; }
.slip-preview { display: block; width: 100%; max-height: 280px; object-fit: contain; margin-top: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--soft); }
.report-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-top: 8px; }
.report-row:first-child { margin-top: 0; }
.report-row strong { text-align: right; }

.badge { border-radius: 999px; padding: .42rem .65rem; }
.status-unpaid { background: #fffbeb; color: #92400e; }
.status-partial { background: #dbeafe; color: #1d4ed8; }
.status-paid { background: #ecfdf3; color: #027a48; }
.status-overdue { background: #fef3f2; color: #b42318; }
.status-cancelled { background: #f2f4f7; color: #475467; }
.required { color: #dc2626; font-weight: 800; }
.quick-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}
.quick-action {
  min-height: 48px;
  display: grid;
  place-items: center;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--blue-dark);
  font-weight: 800;
  text-decoration: none;
  text-align: center;
  box-shadow: 0 10px 24px rgba(16,24,40,.04);
}
.quick-action.primary { background: var(--blue); border-color: var(--blue); color: #fff; }
.quick-action.danger { color: #b42318; border-color: #fecaca; background: #fff5f5; }
.quick-action-form { margin: 0; }
.quick-action-form .quick-action { width: 100%; }
.quick-actions.compact { grid-template-columns: 1fr; margin: 14px 0 0; }
.profile-card h2, .profile-top h2 { margin: 0; font-size: 1.18rem; font-weight: 800; overflow-wrap: anywhere; }
.profile-top { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.profile-top p { margin: 4px 0 0; color: var(--muted); }
.balance-highlight { margin-top: 16px; padding: 16px; border-radius: 8px; background: #eef5ff; }
.balance-highlight span { color: var(--muted); display: block; }
.balance-highlight strong { display: block; margin-top: 4px; font-size: 1.6rem; color: var(--blue-dark); overflow-wrap: anywhere; }
.note-box { background: var(--soft); border-radius: 8px; padding: 14px; }
.note-box p { margin: 6px 0 0; color: var(--muted); }
.record-note { color: var(--muted); margin: 10px 0 0; }
.balance-pill { display: block; color: var(--blue-dark); font-weight: 800; margin-bottom: 6px; white-space: nowrap; }
.mobile-filter { display: none; }
.calendar-page-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 24px;
  align-items: start;
}
.calendar-main,
.calendar-aside,
.calendar-panel,
.calendar-reminder-panel,
#calendar,
#calendar .fc {
  min-width: 0;
  max-width: 100%;
}
.calendar-panel { overflow: hidden; }
.calendar-reminder-panel { position: sticky; top: 96px; }
.calendar-list { display: grid; gap: 12px; }
.calendar-event-card { border: 1px solid var(--line); border-radius: 8px; padding: 12px; background: #fff; }
.calendar-event-card .btn { width: 100%; margin-top: 10px; }
.calendar-mobile-actions { display: none; margin-bottom: 12px; }
.calendar-view-switch { display: none; gap: 8px; margin-bottom: 12px; }
.calendar-view-switch .btn { flex: 1; }
.event-meta { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.event-meta span { background: var(--soft); border-radius: 999px; color: var(--muted); font-size: .82rem; padding: 5px 9px; }
.report-summary { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-bottom: 14px; }
.report-summary div { background: var(--soft); border-radius: 8px; padding: 12px; }
.report-summary span { display: block; color: var(--muted); font-size: .82rem; }
.report-summary strong { display: block; margin-top: 3px; overflow-wrap: anywhere; }

.login-page { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: linear-gradient(145deg, #f8fbff, #eaf3ff); }
.login-card { width: min(430px, 100%); padding: 30px; }
.login-card h1 { font-size: 1.6rem; font-weight: 800; }

.skeleton-scope:not(.loaded) .skeleton-item {
  color: transparent !important;
  border-color: transparent;
  background: linear-gradient(90deg, #eef2f7 25%, #f8fafc 37%, #eef2f7 63%);
  background-size: 400% 100%;
  animation: shimmer 1.1s ease infinite;
  border-radius: 8px;
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }

#calendar .fc-toolbar-title { font-size: 1.1rem; font-weight: 800; }
#calendar .fc-button-primary { background: var(--blue); border-color: var(--blue); }
#calendar { max-width: 100%; overflow: hidden; }
#calendar .fc-event-title, #calendar .fc-list-event-title { overflow-wrap: anywhere; white-space: normal; }
.fc-mobile-event { display: grid; gap: 3px; min-width: 0; }
.fc-mobile-event strong { font-size: .9rem; overflow-wrap: anywhere; }
.fc-mobile-event span { color: var(--muted); font-size: .78rem; overflow-wrap: anywhere; }
.fc-grid-compact-event {
  display: flex;
  align-items: center;
  gap: 3px;
  min-width: 0;
  max-width: 100%;
  overflow: hidden;
}
.fc-grid-compact-event .fc-grid-event-dot {
  width: 6px;
  height: 6px;
  flex: 0 0 6px;
  border-radius: 999px;
  background: currentColor;
}
.fc-grid-compact-event strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.modal { z-index: 1060 !important; }
.modal-backdrop { z-index: 1050 !important; }
.calendar-event-modal { z-index: 1065 !important; }
.calendar-event-modal .modal-dialog { max-width: 720px; }
.calendar-event-modal .modal-content { border: 0; border-radius: 14px; box-shadow: 0 28px 70px rgba(16,24,40,.22); }
.calendar-event-modal .modal-body { padding: 18px; }

@media print {
  .sidebar, .topbar, .bottom-nav, .btn, form { display: none !important; }
  .app-shell { padding-left: 0; }
  body { background: white; }
  .panel { box-shadow: none; border: 0; }
}

@media (max-width: 991px) {
  body { padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
  .app-shell { padding-left: 0; min-width: 0; }
  .sidebar { display: none; }
  .bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 4px;
    padding: 8px max(8px, env(safe-area-inset-left)) calc(8px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-right));
    background: rgba(255,255,255,.96);
    border-top: 1px solid var(--line);
    box-shadow: 0 -12px 32px rgba(16,24,40,.08);
    backdrop-filter: blur(12px);
  }
  .bottom-nav a {
    min-width: 0;
    min-height: 58px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: #667085;
    text-decoration: none;
    border-radius: 8px;
    -webkit-tap-highlight-color: transparent;
  }
  .bottom-nav a.active { background: #eef5ff; color: var(--blue-dark); }
  .bottom-nav a:active, .btn:active, .mobile-record-card:active { transform: scale(.98); }
  .mobile-bottom-nav i {
    font-size: 18px;
    line-height: 1;
    display: block;
    margin-bottom: 4px;
  }
  .mobile-bottom-nav span {
    font-size: 11px;
    line-height: 1.1;
    white-space: nowrap;
  }
  .mobile-bottom-nav a { text-decoration: none; }
  body.modal-open .mobile-bottom-nav,
  body.calendar-modal-open .mobile-bottom-nav {
    display: none;
  }
  .topbar {
    min-height: 66px;
    padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
    gap: 10px;
  }
  .topbar h1 { font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 62vw; }
  .topbar .small { font-size: .72rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 62vw; }
  .app-main { min-height: calc(100vh - 66px); }
  .container-fluid { max-width: 100%; overflow-x: hidden; }
  .panel { padding: 16px; }
  .panel, .stat-card, .login-card { border-radius: 8px; }
  .stat-card { min-height: 96px; padding: 16px; }
  .stat-card strong { font-size: 1.32rem; overflow-wrap: anywhere; }
  .desktop-table { display: none; }
  .mobile-card-list { display: grid; gap: 12px; }
  .mobile-card-list.always { display: grid; }
  .desktop-filter { display: none; }
  .mobile-filter { display: block; }
  .mobile-filter summary {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 10px 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    font-weight: 800;
    color: var(--blue-dark);
  }
  .compact-filter .btn, .report-filter .btn, .report-filter .btn + a { width: 100%; min-width: 0; }
  .panel-head { align-items: stretch; flex-direction: column; }
  .panel-head .btn { width: 100%; }
  .panel form .submit-btn { width: 100%; }
  .list-line { align-items: flex-start; flex-direction: column; }
  .modal-dialog { margin: 8px; max-width: calc(100% - 16px); }
  .modal-content { max-height: calc(100dvh - 16px); overflow: auto; border-radius: 12px; }
  .modal-footer { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .modal-footer .btn { margin: 0 !important; width: 100%; }
  .modal-footer .me-auto { grid-column: 1 / -1; }
  .swal2-popup { width: calc(100vw - 28px) !important; max-width: 420px; }
  .quick-actions { grid-template-columns: 1fr; }
  .report-summary { grid-template-columns: 1fr; }
}

@media (max-width: 992px) {
  .calendar-page-grid {
    grid-template-columns: 1fr;
    gap: 14px;
  }
  .calendar-reminder-panel {
    position: static;
  }
}

@media (max-width: 768px) {
  .calendar-page { --bs-gutter-y: .75rem; }
  .calendar-page-grid {
    gap: 12px;
  }
  .calendar-panel {
    padding: 10px;
    border-color: #dce7f5;
    box-shadow: 0 10px 28px rgba(16, 24, 40, .05);
  }
  .calendar-mobile-actions {
    display: block;
    margin-bottom: 10px;
  }
  .calendar-mobile-actions .btn {
    width: 100%;
    min-height: 42px;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 800;
  }
  .calendar-view-switch {
    display: flex;
    gap: 4px;
    padding: 4px;
    margin-bottom: 10px;
    border: 1px solid #dbe8ff;
    border-radius: 12px;
    background: #eef5ff;
  }
  .calendar-view-switch .btn {
    min-height: 36px;
    border: 0;
    border-radius: 9px;
    font-size: 14px;
    font-weight: 800;
    box-shadow: none;
  }
  .calendar-view-switch .btn-outline-primary {
    background: transparent;
    color: var(--blue-dark);
  }
  .calendar-view-switch .btn-primary {
    background: var(--blue);
  }
  .desktop-add-event { display: none; }
  #calendar {
    width: 100%;
    overflow-x: hidden;
  }
  #calendar .fc-toolbar {
    display: grid;
    grid-template-columns: 76px minmax(0, 1fr) 48px;
    align-items: center;
    gap: 6px;
    margin: 2px 0 10px;
  }
  #calendar .fc-toolbar-chunk {
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: nowrap;
    gap: 5px;
  }
  #calendar .fc-toolbar-chunk:first-child { justify-content: flex-start; }
  #calendar .fc-toolbar-chunk:last-child { justify-content: flex-end; }
  #calendar .fc-toolbar-title {
    width: 100%;
    font-size: 15px;
    font-weight: 800;
    text-align: center;
    line-height: 1.3;
    overflow-wrap: anywhere;
  }
  #calendar .fc-button {
    min-height: 34px;
    padding: 5px 9px;
    font-size: 13px;
    line-height: 1.2;
    border-radius: 9px;
    box-shadow: none;
  }
  #calendar .fc-prev-button,
  #calendar .fc-next-button {
    width: 34px;
    padding-left: 0;
    padding-right: 0;
  }
  #calendar .fc-button-group {
    display: flex;
    gap: 5px;
  }
  #calendar .fc-scrollgrid {
    overflow: hidden;
    border-radius: 10px;
    border-color: #dce7f5;
  }
  #calendar .fc-scrollgrid-sync-table {
    height: auto !important;
  }
  #calendar .fc-view-harness {
    min-height: auto !important;
  }
  #calendar .fc-col-header-cell {
    background: #f4f8ff;
  }
  #calendar .fc-daygrid-day-frame {
    min-height: 48px;
    padding: 2px;
  }
  #calendar .fc-daygrid-day-number {
    font-size: 12px;
    font-weight: 700;
    padding: 4px 5px;
    text-decoration: none;
  }
  #calendar .fc-col-header-cell-cushion {
    font-size: 12px;
    font-weight: 800;
    padding: 7px 2px;
    text-decoration: none;
  }
  #calendar .fc-day-other .fc-daygrid-day-number {
    color: #a9c8f7;
  }
  #calendar .fc-day-today {
    background: #fff8d9;
  }
  #calendar .fc-daygrid-day-events {
    margin: 2px 2px 2px;
    min-height: 0;
  }
  #calendar .fc-daygrid-event {
    max-width: 100%;
    overflow: hidden;
    white-space: nowrap !important;
    text-overflow: ellipsis;
    font-size: 10px;
    line-height: 1.2;
    padding: 1px 3px;
    margin-top: 1px;
    border-radius: 4px;
  }
  #calendar .fc-daygrid-event .fc-event-main,
  #calendar .fc-daygrid-event .fc-event-title,
  #calendar .fc-daygrid-event .fc-mobile-event,
  #calendar .fc-daygrid-event .fc-mobile-event strong {
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    white-space: nowrap !important;
    text-overflow: ellipsis;
    overflow-wrap: normal;
  }
  #calendar .fc-daygrid-event .fc-mobile-event span:not(.fc-grid-event-dot) {
    display: none;
  }
  #calendar .fc-daygrid-event-dot,
  #calendar .fc-grid-event-dot {
    margin-right: 2px;
  }
  #calendar .fc-list-day-cushion,
  #calendar .fc-list-table td {
    padding: 9px 8px;
  }
  #calendar .fc-list-day-cushion {
    background: #f4f8ff;
    color: var(--ink);
    font-weight: 800;
  }
  #calendar .fc-list-event:hover td {
    background: #f8fbff;
  }
  #calendar .fc-list-event-time {
    width: 52px;
    font-size: 12px;
    white-space: nowrap;
  }
  #calendar .fc-list-event-title {
    min-width: 0;
  }
  .calendar-event-modal .modal-dialog {
    width: calc(100% - 24px);
    max-width: none;
    margin: 12px auto;
    height: calc(100dvh - 24px);
    max-height: calc(100dvh - 24px);
    display: flex;
    align-items: stretch;
  }
  .calendar-event-modal .modal-content {
    max-height: 100%;
    display: flex;
    flex-direction: column;
    border-radius: 18px;
    overflow: hidden;
  }
  .calendar-event-modal .modal-header {
    flex: 0 0 auto;
  }
  .calendar-event-modal .modal-body {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .calendar-event-modal .modal-footer {
    flex: 0 0 auto;
    position: sticky;
    bottom: 0;
    background: #fff;
    z-index: 2;
  }
}

@media (min-width: 992px) {
  .bottom-nav { display: none; }
  .mobile-card-list.always { display: grid; }
  .desktop-filter { display: flex; }
}

@media (max-width: 575px) {
  .container-fluid { padding-left: 12px; padding-right: 12px; }
  .py-4 { padding-top: .9rem !important; padding-bottom: .9rem !important; }
  .mini-grid { grid-template-columns: 1fr; }
  .record-grid { grid-template-columns: 1fr; }
  .record-actions { grid-template-columns: 1fr; }
  .panel form > .d-flex { flex-direction: column; }
  .panel form > .d-flex .btn { width: 100%; }
  .login-page { align-items: start; padding: calc(28px + env(safe-area-inset-top)) 14px 14px; }
  .login-card { padding: 22px; }
  .report-row { flex-direction: column; gap: 3px; }
  .report-row strong { text-align: left; }
}
