/* ── Vazirmatn Font (Persian) ─────────────────────────── */
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;600;700&display=swap');

/* ── Custom Scrollbar ─────────────────────────────────── */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track {
  background: var(--bg);
}
::-webkit-scrollbar-thumb {
  background: var(--scroll-thumb);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--scroll-thumb-hover);
}

/* ── Reset & Base ─────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #232733;
  --border: #2e3345;
  --text: #e4e6f0;
  --text-dim: #8b8fa3;
  --accent: #6c8cff;
  --accent-hover: #8aa4ff;
  --green: #4ade80;
  --red: #f87171;
  --orange: #fb923c;
  --purple: #c084fc;
  --scroll-thumb: #3e4460;
  --scroll-thumb-hover: #5a6080;
  --radius: 10px;
  --shadow: 0 4px 24px rgba(0,0,0,.3);
  --sidebar-width: 320px;
}

/* ── Light Mode ───────────────────────────────────────── */
[data-theme="light"] {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface2: #eef0f5;
  --border: #d8dbe6;
  --text: #1a1d27;
  --text-dim: #6b7280;
  --accent: #5b7aee;
  --accent-hover: #6c8cff;
  --scroll-thumb: #b0b5c8;
  --scroll-thumb-hover: #8a90a5;
  --shadow: 0 4px 24px rgba(0,0,0,.08);
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

[dir="rtl"] body,
[dir="rtl"] * {
  font-family: 'Vazirmatn', 'Segoe UI', system-ui, sans-serif;
}

/* ── App Layout (Sidebar + Main) ──────────────────────── */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* ── Sidebar ──────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sidebar-header h1 {
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}

/* ── Theme Toggle ─────────────────────────────────────── */
.theme-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  opacity: 0.6;
  transition: opacity .2s;
  padding: 0;
  flex-shrink: 0;
}
.theme-btn svg {
  width: 18px;
  height: 18px;
  display: block;
}
.theme-btn:hover {
  opacity: 1;
}

/* ── Search Panel (inside sidebar) ────────────────────── */
.search-panel {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.form-group {
  margin-bottom: 14px;
}

.form-group label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 9px 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.88rem;
  outline: none;
  transition: border-color .2s;
}
.form-group input:focus,
.form-group select:focus {
  border-color: var(--accent);
}

/* ── Styled Time Input ────────────────────────────────── */
.tp-wrapper {
  position: relative;
}
.tp-display {
  width: 100%;
  padding: 9px 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.88rem;
  font-family: 'Vazirmatn', sans-serif;
  outline: none;
  cursor: pointer;
  transition: border-color .2s;
}
.tp-display:focus {
  border-color: var(--accent);
}

.form-actions {
  display: flex;
  gap: 8px;
  margin-top: 18px;
}

button {
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s, transform .1s;
}
button:active { transform: scale(0.97); }

#searchBtn {
  background: var(--accent);
  color: #fff;
  flex: 1;
}
#searchBtn:hover { background: var(--accent-hover); }
#searchBtn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#clearBtn {
  background: var(--surface2);
  color: var(--text-dim);
  border: 1px solid var(--border);
}
#clearBtn:hover { color: var(--text); }

/* ── Main Content ─────────────────────────────────────── */
.main-content {
  flex: 1;
  margin-right: var(--sidebar-width);
  padding: 24px;
  min-width: 0;
}

/* ── Results Panel ────────────────────────────────────── */
.results-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.results-header {
  display: flex;
  justify-content: space-between;
  padding: 14px 24px;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
  color: var(--text-dim);
}
#resultCount { font-weight: 600; color: var(--text); }

/* ── Error ────────────────────────────────────────────── */
.error {
  padding: 16px 24px;
  background: rgba(248,113,113,.1);
  border-left: 3px solid var(--red);
  color: var(--red);
  font-size: 0.92rem;
}

/* ── Table ────────────────────────────────────────────── */
.table-wrapper {
  overflow-x: auto;
}

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

thead th {
  padding: 12px 16px;
  text-align: right;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 1;
}

thead th:first-child {
  text-align: center;
  width: 44px;
  min-width: 44px;
}

/* ── Sortable Column Headers ─────────────────────────── */
.sortable {
  cursor: pointer !important;
  user-select: none;
  transition: color .15s, background .15s;
}
.sortable:hover {
  color: var(--accent) !important;
  background: rgba(108, 140, 255, 0.15) !important;
}
.sortable.active {
  color: var(--accent) !important;
}
.sort-icon {
  display: inline-block;
  margin-right: 4px;
  font-size: 0.75rem;
  opacity: 0.25;
  transition: opacity .15s;
}
.sortable:hover .sort-icon {
  opacity: 1 !important;
}
.sortable.active .sort-icon {
  opacity: 1 !important;
  color: var(--accent);
}

thead th:nth-child(2) { text-align: right; }  /* Date */
thead th:nth-child(3) { text-align: right; }  /* Time */
thead th:nth-child(4) { text-align: right; }  /* Duration */
thead th:nth-child(5) { text-align: right; }  /* Dialed */
thead th:nth-child(6) { text-align: right; }  /* Caller */
thead th:nth-child(7) { text-align: right; }  /* Type badge */
thead th:nth-child(8) { text-align: right; }  /* Card/Ch */

.row-num {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 600;
  width: 50px;
  min-width: 50px;
}

tbody td {
  padding: 10px 16px;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  text-align: right;
}

tbody td:first-child { text-align: center; }       /* # */
tbody td:nth-child(2)  { text-align: right; }     /* Date */
tbody td:nth-child(3)  { text-align: right; }     /* Time */
tbody td:nth-child(4)  { text-align: right; }     /* Duration */
tbody td:nth-child(5)  { text-align: right; }     /* Dialed */
tbody td:nth-child(6)  { text-align: right; }     /* Caller */
tbody td:nth-child(7)  { text-align: right; }     /* Type badge */
tbody td:nth-child(8)  { text-align: right; }     /* Card/Ch */

tbody tr:hover {
  background: rgba(108,140,255,.06);
}

.empty-row td {
  text-align: center;
  color: var(--text-dim);
  padding: 48px 16px;
  font-size: 0.95rem;
}

/* ── Call Type Badges ─────────────────────────────────── */
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 0.8rem;
  font-weight: 600;
}
.badge-incoming  { background: rgba(74,222,128,.15); color: var(--green); }
.badge-outgoing  { background: rgba(108,140,255,.15); color: var(--accent); }
.badge-missed    { background: rgba(248,113,113,.15); color: var(--red); }
.badge-other     { background: rgba(251,146,60,.15);  color: var(--orange); }

/* ── Scroll sentinel (invisible, for IntersectionObserver) ─ */
.scroll-sentinel {
  height: 0;
  width: 100%;
  overflow: hidden;
  pointer-events: none;
}

/* ── Spinner ──────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  vertical-align: middle;
  margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Loading Row ──────────────────────────────────────── */
.loading-row td {
  text-align: center;
  color: var(--text-dim);
  padding: 20px 16px;
  font-size: 0.92rem;
}

/* ── Jalali Date Picker ───────────────────────────────── */
.jp-picker {
  position: fixed;
  z-index: 9999;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
  width: 280px;
  direction: ltr;
  font-family: 'Vazirmatn', sans-serif;
  user-select: none;
}
.jp-cal { padding: 12px; direction: rtl; }
.jp-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-bottom: 10px;
  direction: ltr;
}
.jp-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--accent);
  min-width: 120px;
  text-align: center;
  direction: rtl;
}
.jp-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 4px 8px;
  font-size: 0.85rem;
  cursor: pointer;
  line-height: 1;
  unicode-bidi: isolate;
}
.jp-btn:hover { background: var(--accent); color: #fff; }
.jp-days {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
  margin-bottom: 4px;
}
.jp-day-name {
  text-align: center;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-dim);
  padding: 4px 0;
}
.jp-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 2px;
}
.jp-cell {
  text-align: center;
  padding: 7px 0;
  font-size: 0.85rem;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
  transition: background .15s;
}
.jp-cell:hover { background: rgba(108,140,255,.15); }
.jp-cell.jp-sel {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}
.jp-cell.jp-sel:hover { background: var(--accent-hover); }
.jp-empty { cursor: default; }

.jp-today-btn {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 8px 0;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-family: 'Vazirmatn', sans-serif;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: background .2s;
}
.jp-today-btn:hover {
  background: var(--accent-hover);
}

/* ── Time Picker (Dropdown) ───────────────────────────── */
.tp-picker {
  position: fixed;
  z-index: 9999;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,.5);
  width: 220px;
  font-family: 'Vazirmatn', sans-serif;
  user-select: none;
}
.tp-cal {
  padding: 12px;
  direction: ltr;
}
.tp-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.tp-select {
  flex: 1;
  padding: 8px 6px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1.1rem;
  font-family: 'Vazirmatn', sans-serif;
  text-align: center;
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
}
.tp-select:focus {
  border-color: var(--accent);
}
.tp-sep {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text-dim);
}
.tp-set-btn {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 8px 0;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-family: 'Vazirmatn', sans-serif;
  font-size: 0.88rem;
  font-weight: 700;
  cursor: pointer;
  transition: background .2s;
}
.tp-set-btn:hover {
  background: var(--accent-hover);
}

/* ── Hamburger button (mobile only) ───────────────────── */
.hamburger-btn {
  display: none;
  width: 36px;
  height: 36px;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
}
.hamburger-btn svg {
  width: 22px;
  height: 22px;
  display: block;
}

/* ── Close sidebar button (mobile only) ───────────────── */
.close-sidebar-btn {
  display: none;
  width: 32px;
  height: 32px;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  opacity: 0.7;
  transition: opacity .2s;
  padding: 0;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
}
.close-sidebar-btn svg {
  width: 20px;
  height: 20px;
  display: block;
}
.close-sidebar-btn:hover {
  opacity: 1;
  color: var(--red);
}

/* ── Mobile overlay backdrop ──────────────────────────── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 99;
}
.sidebar-overlay.active {
  display: block;
}

/* ── Responsive: tablets (≤ 900px) ────────────────────── */
@media (max-width: 900px) {
  .sidebar {
    width: 260px;
    min-width: 260px;
  }
  .main-content {
    margin-right: 260px;
    padding: 16px;
  }
}

/* ── Responsive: phones / narrow portrait (≤ 700px) ───── */
@media (max-width: 700px) {

  /* ── Layout: sidebar becomes slide-in drawer ────────── */
  .hamburger-btn {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .app-layout {
    flex-direction: column;
  }

  .sidebar {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 85%;
    max-width: 320px;
    min-width: unset;
    z-index: 100;
    transform: translateX(100%);
    transition: transform .25s ease;
    border-left: 1px solid var(--border);
  }
  .sidebar.open {
    transform: translateX(0);
  }

  .close-sidebar-btn {
    display: flex;
  }

  .sidebar-header {
    padding: 14px 16px;
  }
  .sidebar-header h1 {
    font-size: 1.1rem;
  }

  .user-info-bar {
    padding: 6px 16px;
    font-size: 0.72rem;
  }

  .search-panel {
    padding: 12px;
  }

  .form-group {
    margin-bottom: 10px;
  }
  .form-group label {
    font-size: 0.68rem;
    margin-bottom: 4px;
  }
  .form-group input,
  .form-group select {
    padding: 10px 12px;
    font-size: 1rem;
  }

  .form-actions {
    margin-top: 14px;
  }
  .form-actions button {
    padding: 12px 16px;
    font-size: 1rem;
  }

  /* ── Main content fills screen ──────────────────────── */
  .main-content {
    margin-right: 0;
    padding: 10px;
  }

  /* ── Results header compact ─────────────────────────── */
  .results-header {
    padding: 10px 14px;
    font-size: 0.8rem;
  }

  /* ── Table → card layout on narrow screens ──────────── */
  .results-panel {
    border-radius: 0;
    border-left: none;
    border-right: none;
    min-height: calc(100vh - 60px);
  }

  .table-wrapper {
    overflow-x: visible;
  }

  table,
  thead,
  tbody,
  tr,
  td,
  th {
    display: block;
  }

  thead {
    position: absolute;
    width: 0;
    height: 0;
    overflow: hidden;
    clip: rect(0,0,0,0);
  }

  tbody tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 12px 14px;
    box-shadow: 0 1px 4px rgba(0,0,0,.15);
  }

  tbody td {
    padding: 5px 0;
    border-bottom: none;
    text-align: left !important;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.88rem;
  }
  tbody td::before {
    content: attr(data-label);
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--text-dim);
    letter-spacing: 0.3px;
    flex-shrink: 0;
    margin-left: 10px;
  }

  /* row-num stacks on top */
  tbody td:first-child {
    justify-content: flex-start;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 4px;
  }
  tbody td:first-child::before { display: none; }

  /* empty / loading rows */
  .empty-row,
  .loading-row {
    display: table-row !important;
  }
  .empty-row td,
  .loading-row td {
    display: table-cell !important;
    text-align: center;
    padding: 32px 12px !important;
  }
  .empty-row td::before,
  .loading-row td::before {
    display: none;
  }

  /* ── Pickers full-width on mobile ───────────────────── */
  .jp-picker,
  .tp-picker {
    width: calc(100vw - 24px);
    left: 12px !important;
  }

  /* ── Login card full-width ──────────────────────────── */
  .login-card {
    max-width: 100%;
    margin: 0 12px;
    padding: 24px 20px;
  }
  .login-header h1 {
    font-size: 1.2rem;
  }
}

/* ── Responsive: very narrow (≤ 400px) ────────────────── */
@media (max-width: 400px) {
  .sidebar {
    width: 100%;
    max-width: 100%;
  }

  .sidebar-header h1 {
    font-size: 1rem;
  }

  .form-group input,
  .form-group select {
    font-size: 0.95rem;
    padding: 10px 10px;
  }

  .form-actions {
    flex-direction: column;
  }
  .form-actions button {
    width: 100%;
  }

  tbody td {
    font-size: 0.82rem;
  }
  tbody td::before {
    font-size: 0.68rem;
  }
}
