/* ═══════════════════════════════════════════════════════════════
   CORE.CSS — Structural & Layout (Non-skinnable)
   
   This file contains layout, positioning, and structural rules
   that should NOT be replaced when re-skinning the UI.
   All visual theming (colors, fonts, shadows, borders, radii)
   lives in skin.css.
   ═══════════════════════════════════════════════════════════════ */

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ─── Hidden attribute override (must beat display rules) ───── */
[hidden] {
  display: none !important;
}

/* ─── Login Screen ──────────────────────────────────────────── */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.login-card {
  width: 100%;
  max-width: 380px;
  padding: 40px 32px;
}

.login-logo {
  text-align: center;
  margin-bottom: 4px;
}

.login-subtitle {
  text-align: center;
  margin-bottom: 32px;
}

/* ─── App Shell ─────────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-columns: 240px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ─── Sidebar ───────────────────────────────────────────────── */
#sidebar {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow-y: auto;
  z-index: 10;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 20px 16px;
  flex-shrink: 0;
}

.brand-mark {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex-shrink: 0;
}

#sidebar-nav {
  flex: 1;
  padding: 0 12px;
  overflow-y: auto;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  text-decoration: none;
  cursor: pointer;
  position: relative;
}

.nav-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.nav-section-label {
  padding: 20px 12px 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  user-select: none;
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px;
  flex-shrink: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.user-avatar {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-transform: uppercase;
}

.user-details {
  min-width: 0;
}

.user-name, .user-role {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ─── Main Content ──────────────────────────────────────────── */
#main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

#page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 32px 0;
  flex-shrink: 0;
}

#page-header h1 {
  line-height: 1.2;
}

.subtitle {
  margin-top: 2px;
}

#page-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

#page-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 32px;
  flex-shrink: 0;
  gap: 16px;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

#toolbar-filters {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.search-box {
  position: relative;
  max-width: 300px;
  flex: 1;
}

.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
}

.search-box input {
  width: 100%;
  padding: 7px 12px 7px 34px;
}

#page-content {
  flex: 1;
  overflow-y: auto;
  padding: 0 32px 32px;
}

/* ─── Tables ────────────────────────────────────────────────── */
.table-wrap {
  width: 100%;
}

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

th {
  text-align: left;
  white-space: nowrap;
  user-select: none;
  position: sticky;
  top: 0;
  z-index: 1;
}

th, td {
  padding: 10px 16px;
}

td {
  vertical-align: top;
}

.col-actions {
  width: 1%;
  white-space: nowrap;
  text-align: right;
}

.action-group {
  display: flex;
  gap: 4px;
  justify-content: flex-end;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 64px 32px;
  text-align: center;
}

.empty-state svg {
  margin-bottom: 16px;
}

.load-more-wrap {
  display: flex;
  justify-content: center;
  padding: 16px 0;
}

/* ─── Badges / Tags ─────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  white-space: nowrap;
}

.badge + .badge {
  margin-left: 4px;
}

.badge-group {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

/* ─── Buttons ───────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  cursor: pointer;
  border: none;
  white-space: nowrap;
  text-decoration: none;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-sm {
  padding: 4px 10px;
}

.btn-full {
  width: 100%;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px;
}

/* ─── Forms ─────────────────────────────────────────────────── */
.form-group {
  margin-bottom: 16px;
}

.form-group:last-child {
  margin-bottom: 0;
}

.form-group label {
  display: block;
  margin-bottom: 4px;
}

.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"],
.form-group input[type="number"],
.form-group input[type="date"],
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 8px 12px;
}

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

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

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

.form-hint {
  margin-top: 4px;
}

.form-error {
  margin-top: 4px;
}

.form-error[hidden] {
  display: none;
}

/* Checkbox group */
.checkbox-group {
  display: flex;
  gap: 16px;
  padding-top: 4px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin: 0;
  cursor: pointer;
  margin-right: 5px;
}

/* Toggle */
.toggle-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  white-space: nowrap;
}

.toggle-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
}

/* ─── Modal ─────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px 24px;
  z-index: 100;
  overflow-y: auto;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  width: 100%;
  max-width: 640px;
  max-height: calc(100vh - 48px);
  display: flex;
  flex-direction: column;
  position: relative;
}

.modal.modal-lg {
  max-width: min(1200px, calc(100vw - 48px));
}

.modal-sm {
  max-width: 440px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 0;
  flex-shrink: 0;
}

.modal-body {
  padding: 20px 24px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 16px 24px;
  flex-shrink: 0;
}

/* ─── Toasts ────────────────────────────────────────────────── */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 200;
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 12px 20px;
  min-width: 260px;
  max-width: 420px;
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

/* ─── Repeater (for dynamic sub-forms like book formats) ───── */
.repeater-item {
  position: relative;
  padding: 16px;
  margin-bottom: 12px;
}

.repeater-item .repeater-remove {
  position: absolute;
  top: 8px;
  right: 8px;
}

.repeater-add {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  background: none;
  border: none;
  padding: 8px 0;
}

/* ─── Section / Divider ─────────────────────────────────────── */
.form-section-title {
  margin: 20px 0 12px;
  padding-bottom: 6px;
}

.form-section-title:first-child {
  margin-top: 0;
}

/* ─── Detail list (for read-only info) ──────────────────────── */
.detail-grid {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 6px 16px;
  align-items: baseline;
}

.detail-grid-2col {
  display: grid;
  grid-template-columns: 140px 1fr 140px 1fr;
  gap: 6px 16px;
  align-items: baseline;
}

.detail-label {
  white-space: nowrap;
}

.detail-span-full {
  grid-column: 1 / -1;
}

/* ─── Autocomplete ───────────────────────────────────────── */
.autocomplete-wrap {
  position: relative;
  display: inline-block;
}

.autocomplete-wrap input[type="text"] {
  width: 100%;
}

.autocomplete-dropdown {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  z-index: 50;
  max-height: 240px;
  overflow-y: auto;
  margin-top: 2px;
}

.autocomplete-option {
  cursor: pointer;
}

/* ─── Busy loading cursor ───────────────────────────────────── */
body.busy-loading,
body.busy-loading * {
  cursor: progress !important;
}

/* ─── Spinner ───────────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.page-spinner {
  display: flex;
  justify-content: center;
  padding: 64px;
}

/* ─── Archived row ──────────────────────────────────────────── */
tr.archived td {
  opacity: 0.55;
}

tr.clickable-row {
  cursor: pointer;
}

.sortable-th:hover {
  opacity: 0.75;
}

/* ─── Utility ───────────────────────────────────────────────── */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}

.text-right {
  text-align: right;
}

.text-mono {
  font-variant-numeric: tabular-nums;
}

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.mt-0 { margin-top: 0; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.gap-8 { gap: 8px; }

/* ─── Dashboard ─────────────────────────────────────────────── */
.dash-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.dash-card {
  display: flex;
  flex-direction: column;
  padding: 20px;
  text-decoration: none;
  min-width: 0;
}

.dash-card-label {
  margin-bottom: 6px;
}

.dash-card-value {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dash-grid {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.dash-panel {
  padding: 0;
  overflow: hidden;
}

.dash-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 12px;
}

.dash-panel-title {
  line-height: 1.3;
}

.dash-panel table {
  margin: 0;
}

.dash-panel th,
.dash-panel td {
  padding: 8px 20px;
}

.dash-empty-msg {
  padding: 32px 20px;
  text-align: center;
}

.dash-tables {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}

.dash-chart-wrap {
  padding: 0 20px 20px;
}

/* ─── Bar Chart ─────────────────────────────────────────────── */
.dash-bars-container {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  height: 220px;
}

.dash-bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  min-width: 0;
}

.dash-bar-value {
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  height: 20px;
  flex-shrink: 0;
}

.dash-bar-track {
  flex: 1;
  width: 100%;
  max-width: 48px;
  display: flex;
  align-items: flex-end;
  min-height: 0;
}

.dash-bar-fill {
  width: 100%;
  min-height: 2px;
}

.dash-bar-label {
  text-align: center;
  white-space: nowrap;
  padding-top: 6px;
  flex-shrink: 0;
}

/* ─── Dashboard responsive ──────────────────────────────────── */
@media (max-width: 960px) {
  .dash-tables {
    grid-template-columns: 1fr;
  }
}
