/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f5f3;
  --surface: #ffffff;
  --surface2: #f0ede8;
  --border: #e2ddd8;
  --text: #1a1916;
  --text-2: #6b6560;
  --text-3: #9b958f;
  --primary: #1a1916;
  --primary-hover: #333028;
  --accent: #d4500a;
  --success: #1a7a4a;
  --warning: #b07b00;
  --danger: #c0392b;
  --sidebar-w: 220px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,.1);
  --font: 'IBM Plex Sans Thai', sans-serif;
  --mono: 'IBM Plex Mono', monospace;
}

html { font-size: 14px; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
  line-height: 1.5;
}

/* ===== SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-w);
  background: var(--primary);
  color: #fff;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0;
  height: 100vh;
  z-index: 100;
  transition: transform 0.2s ease;
}

.sidebar-logo {
  padding: 1.5rem 1.25rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,.1);
}
.logo-mark {
  display: block;
  font-family: var(--mono);
  font-size: 1.25rem;
  font-weight: 500;
  letter-spacing: .05em;
  color: #fff;
}
.logo-sub {
  font-size: .75rem;
  color: rgba(255,255,255,.45);
  letter-spacing: .04em;
}

.sidebar-nav {
  flex: 1;
  padding: .75rem 0;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: .625rem;
  padding: .625rem 1.25rem;
  color: rgba(255,255,255,.6);
  text-decoration: none;
  font-size: .875rem;
  font-weight: 400;
  border-left: 2px solid transparent;
  transition: all .15s;
}
.nav-item svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 1.75; flex-shrink: 0; }
.nav-item:hover { color: #fff; background: rgba(255,255,255,.06); }
.nav-item.active { color: #fff; border-left-color: var(--accent); background: rgba(255,255,255,.08); font-weight: 500; }

.sidebar-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid rgba(255,255,255,.08);
  font-family: var(--mono);
  font-size: .75rem;
  color: rgba(255,255,255,.3);
}

/* ===== MAIN ===== */
.main-content {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ===== TOPBAR ===== */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 1.5rem;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 50;
}
.menu-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: .25rem;
}
.menu-toggle svg { width: 20px; height: 20px; stroke: var(--text); fill: none; stroke-width: 1.75; }
.page-title { font-size: 1rem; font-weight: 600; flex: 1; }
.topbar-actions { display: flex; gap: .5rem; }

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .375rem;
  padding: .5rem 1rem;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-family: var(--font);
  font-size: .8125rem;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-outline { background: transparent; border-color: var(--border); color: var(--text); }
.btn-outline:hover { background: var(--surface2); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { opacity: .88; }
.btn-danger { background: var(--danger); color: #fff; border: none; }
.btn-sm { padding: .3rem .625rem; font-size: .75rem; }

/* ===== PAGE CONTENT ===== */
.page-content { flex: 1; padding: 1.5rem; }
.page { display: none; }
.page.active { display: block; }

/* ===== STATS ROW ===== */
.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}
.stat-label { font-size: .75rem; color: var(--text-2); margin-bottom: .25rem; }
.stat-value { font-size: 1.75rem; font-weight: 600; font-family: var(--mono); line-height: 1; }
.stat-card.danger .stat-value { color: var(--danger); }
.stat-card.warning .stat-value { color: var(--warning); }
.stat-card.success .stat-value { color: var(--success); }

/* ===== SECTION HEADER ===== */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: .5rem;
}
.section-header h2 { font-size: .9375rem; font-weight: 600; }
.filters { display: flex; gap: .5rem; flex-wrap: wrap; }
.filters select, .filters input {
  padding: .375rem .625rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-family: var(--font);
  font-size: .8125rem;
  color: var(--text);
}

/* ===== TABLE ===== */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: .8125rem;
}
.data-table th {
  padding: .625rem 1rem;
  text-align: left;
  font-weight: 500;
  color: var(--text-2);
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.data-table td {
  padding: .75rem 1rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--bg); }

/* ===== BADGES ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: .2rem .5rem;
  border-radius: 4px;
  font-size: .7rem;
  font-weight: 500;
  white-space: nowrap;
}
.badge-expired { background: #fde8e6; color: #c0392b; }
.badge-critical { background: #fde8e6; color: #c0392b; }
.badge-warning { background: #fef3cd; color: #856404; }
.badge-caution { background: #fff3cd; color: #b07b00; }
.badge-ok { background: #d4edda; color: #155724; }
.badge-renewed { background: #d4edda; color: #155724; }

/* Days remaining coloring */
.days-expired { color: var(--danger); font-weight: 600; }
.days-critical { color: var(--danger); font-weight: 600; }
.days-warning { color: var(--warning); font-weight: 600; }
.days-caution { color: var(--warning); }
.days-ok { color: var(--text-2); }
.days-renewed { color: var(--success); font-style: italic; }

/* ===== ACTION BUTTONS IN TABLE ===== */
.action-group { display: flex; gap: .375rem; flex-wrap: wrap; }

/* ===== CARDS GRID ===== */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
}
.sub-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  position: relative;
  border-left: 3px solid var(--border);
}
.sub-card.expired { border-left-color: var(--danger); }
.sub-card.critical { border-left-color: var(--danger); }
.sub-card.warning { border-left-color: var(--warning); }
.sub-card.caution { border-left-color: #f0ad4e; }
.sub-card.ok { border-left-color: var(--success); }
.sub-card.renewed { border-left-color: var(--success); }

.sub-card-name { font-weight: 600; font-size: .9375rem; margin-bottom: .25rem; }
.sub-card-company { font-size: .75rem; color: var(--text-2); margin-bottom: .75rem; }
.sub-card-info { font-size: .8125rem; color: var(--text-2); margin-bottom: .5rem; display: flex; flex-direction: column; gap: .25rem; }
.sub-card-info span { display: flex; justify-content: space-between; }
.sub-card-info strong { color: var(--text); }
.sub-card-actions { margin-top: 1rem; display: flex; gap: .375rem; flex-wrap: wrap; }
.sub-card-detail { font-size: .75rem; color: var(--text-3); margin-top: .5rem; line-height: 1.4; }

/* ===== COMPANIES LIST ===== */
.companies-list { display: flex; flex-direction: column; gap: .5rem; }
.company-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .875rem 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.company-name { font-weight: 500; }
.company-count { font-size: .75rem; color: var(--text-3); }

/* ===== SETTINGS ===== */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
  max-width: 800px;
}
.settings-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
}
.settings-card h3 { font-size: .9375rem; font-weight: 600; margin-bottom: .375rem; }
.settings-desc { font-size: .8125rem; color: var(--text-2); margin-bottom: 1.25rem; }
.settings-actions { display: flex; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; }
.settings-msg { margin-top: .75rem; font-size: .8125rem; }
.settings-msg.ok { color: var(--success); }
.settings-msg.err { color: var(--danger); }

.threshold-list { display: flex; flex-direction: column; gap: .625rem; }
.threshold-item { display: flex; align-items: center; gap: .5rem; font-size: .875rem; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.dot.red { background: var(--danger); }
.dot.orange { background: #e67e22; }
.dot.yellow { background: var(--warning); }

/* ===== FORMS ===== */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-size: .8125rem; font-weight: 500; margin-bottom: .375rem; }
.req { color: var(--danger); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: .5rem .75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-family: var(--font);
  font-size: .875rem;
  color: var(--text);
  transition: border-color .15s;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.form-group textarea { resize: vertical; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.input-group { display: flex; gap: .5rem; }
.input-group input { flex: 1; }
.input-group select { width: 110px; flex-shrink: 0; }

/* ===== MODAL ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.45);
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.modal-overlay.open { display: flex; }
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  width: 100%;
  max-width: 560px;
  box-shadow: var(--shadow-md);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.modal-sm { max-width: 400px; }
.modal-header {
  padding: 1.25rem 1.5rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h3 { font-size: 1rem; font-weight: 600; }
.modal-close { background: none; border: none; font-size: 1.25rem; cursor: pointer; color: var(--text-3); line-height: 1; }
.modal-close:hover { color: var(--text); }
.modal-body { padding: 1.25rem 1.5rem; overflow-y: auto; flex: 1; }
.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: .5rem;
}

/* ===== EMPTY STATE ===== */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-3);
}
.empty-state svg {
  width: 40px; height: 40px;
  stroke: var(--text-3); fill: none; stroke-width: 1.5;
  margin-bottom: .75rem;
}
.empty-state p { font-size: .875rem; }

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  background: var(--primary);
  color: #fff;
  padding: .75rem 1.25rem;
  border-radius: var(--radius);
  font-size: .875rem;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transform: translateY(8px);
  transition: all .2s;
  pointer-events: none;
  z-index: 999;
  max-width: 320px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ===== LOADING ===== */
.loading-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(245,245,243,.7);
  z-index: 999;
  align-items: center;
  justify-content: center;
}
.loading-overlay.active { display: flex; }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== RESPONSIVE ===== */
/* Sidebar Backdrop for Mobile */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 90;
  backdrop-filter: blur(2px);
  transition: opacity 0.3s ease;
}

@media (max-width: 1024px) {
  /* Tablet Adjustments */
  .stats-row { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .sidebar { 
    transform: translateX(-100%); 
    box-shadow: var(--shadow-md); 
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar.open ~ .sidebar-backdrop { display: block; }
  .main-content { margin-left: 0; width: 100%; }
  .menu-toggle { display: block; }
  .form-row { grid-template-columns: 1fr; }
  .topbar { padding: 0 1rem; }
  .page-content { padding: 1rem; }
  
  /* Make table more compact */
  .data-table th, .data-table td { padding: .5rem .75rem; }
}

@media (max-width: 480px) {
  /* Mobile Adjustments */
  .stats-row { grid-template-columns: 1fr; }
  .section-header { flex-direction: column; align-items: stretch; }
  .filters { width: 100%; display: grid; grid-template-columns: 1fr; }
  .filters select { width: 100%; }
  
  /* Action buttons in tables stacked */
  .action-group { flex-direction: column; width: 100%; }
  .action-group .btn { width: 100%; justify-content: center; }
  
  /* Companies page adjustments */
  .company-row { flex-direction: column; align-items: flex-start; gap: 1rem; }
  .company-row .btn { align-self: stretch; width: 100%; justify-content: center; }
  
  /* Topbar */
  .page-title { font-size: 1.1rem; }
  .topbar-actions .btn { padding: .4rem .6rem; font-size: .75rem; }
  
  /* Sub Cards */
  .sub-card-actions { flex-direction: column; }
  .sub-card-actions .btn { width: 100%; justify-content: center; }
}
