/* ==========================================================================
   TỬ VI MODERN - HỆ THỐNG AN SAO TỬ VI ỨNG DỤNG BỬU ĐÌNH
   Font Be Vietnam Pro Chuẩn Tiếng Việt, Thanh Nhã, Không Tràn Viền
   ========================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:ital,wght@0,300;0,400;0,500;0,600;0,700;0,800;1,400;1,600&display=swap');

:root {
  /* Giao diện Sáng (Light Mode duy nhất) */
  --bg-primary: #f8fafc;
  --bg-secondary: #ffffff;
  --bg-card: #ffffff;
  --bg-glass: #f1f5f9;
  --bg-subtle: #f8fafc;
  --border-color: #cbd5e1;
  --border-dark: #334155;
  --border-gold: #d97706;

  --text-primary: #0f172a;
  --text-secondary: #334155;
  --text-muted: #64748b;

  --gold-primary: #b45309;
  --gold-accent: #d97706;
  --gold-glow: rgba(217, 119, 6, 0.15);

  /* Ngũ Hành Chuẩn Phong Thủy */
  --kim-color: #5a5a5a;
  --moc-color: #037100;
  --thuy-color: #000000;
  --hoa-color: #dc2626;
  --tho-color: #d97706;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 10px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.12);
  --shadow-gold: 0 0 12px rgba(217, 119, 6, 0.25);

  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-full: 9999px;

  /* Font Be Vietnam Pro chuẩn thiết kế cho tiếng Việt */
  --font-family: 'Be Vietnam Pro', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'Consolas', 'Courier New', monospace;
}

/* Base Styles */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* Top App Header */
.app-header {
  background: var(--bg-secondary);
  border-bottom: 2px solid var(--border-color);
  position: sticky;
  top: 0;
  z-index: 100;
  padding: 10px 20px;
  box-shadow: var(--shadow-sm);
}

.header-container {
  max-width: 1600px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 15px;
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-logo-badge {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, #d97706, #b45309);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 20px;
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
}

.brand-title {
  min-width: 0;
}

.brand-title h1 {
  font-family: var(--font-family);
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: var(--gold-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-title .brand-sub {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.btn-header {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 7px 14px;
  border-radius: var(--radius-md);
  font-family: var(--font-family);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
  flex-shrink: 0;
}

.btn-header:hover {
  background: var(--gold-glow);
  border-color: var(--gold-primary);
  color: var(--gold-primary);
}

.btn-header.primary {
  background: linear-gradient(135deg, #d97706, #b45309);
  border: none;
  color: #ffffff;
  box-shadow: var(--shadow-sm);
}

.btn-header.primary:hover {
  box-shadow: var(--shadow-gold);
  transform: translateY(-1px);
}

.badge-count {
  background: rgba(0, 0, 0, 0.1);
  padding: 2px 7px;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
}

/* Main Layout */
.main-wrapper {
  max-width: 1400px;
  margin: 15px auto;
  padding: 0 15px;
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  gap: 20px;
  flex: 1;
  width: 100%;
  box-sizing: border-box;
}

@media (max-width: 1100px) {
  .main-wrapper {
    grid-template-columns: 1fr;
  }
}

/* Sidebar / Input Form Panel */
.form-panel-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  gap: 15px;
  height: fit-content;
}

.panel-header {
  border-bottom: 2px solid var(--border-color);
  padding-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-header h2 {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--gold-primary);
}

.quick-fill-row {
  display: flex;
  gap: 6px;
}

.btn-pill-small {
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  font-family: var(--font-family);
  font-size: 0.75rem;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-pill-small:hover {
  border-color: var(--gold-primary);
  color: var(--gold-primary);
  background: var(--gold-glow);
}

/* Form Groups */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-group label {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  line-height: 1.4;
  margin-bottom: 2px;
}

.input-text, .select-custom {
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  padding: 8px 10px;
  font-size: 0.88rem;
  font-family: var(--font-family);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  width: 100%;
}

.input-text:focus, .select-custom:focus {
  border-color: var(--gold-primary);
  box-shadow: 0 0 0 3px var(--gold-glow);
}

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

.form-row-3 {
  display: grid;
  grid-template-columns: 0.85fr 1.35fr 1.15fr;
  gap: 6px;
}

.form-row-3 .select-custom {
  padding: 8px 6px;
  font-size: 0.84rem;
}

/* Calendar Switch Pill */
.calendar-switch-container {
  display: flex;
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-full);
  padding: 3px;
  margin-top: 4px;
}

.calendar-switch-label {
  flex: 1;
  text-align: center;
  padding: 6px;
  border-radius: var(--radius-full);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all 0.2s;
}

.calendar-switch-label.active {
  background: var(--gold-primary);
  color: #fff;
  box-shadow: var(--shadow-sm);
}

/* Big An Sao Button */
.btn-an-sao {
  background: linear-gradient(135deg, #d97706, #b45309);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-md);
  padding: 13px;
  font-size: 1rem;
  font-family: var(--font-family);
  font-weight: 700;
  letter-spacing: 0.5px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}

.btn-an-sao:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-gold);
}

/* Viewer Section (Right Column) */
.viewer-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  width: 100%;
  box-sizing: border-box;
}

#gridSection, #slideSection {
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

/* Viewer Toolbar */
.viewer-toolbar {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 8px 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  box-shadow: var(--shadow-sm);
}

.view-mode-toggle {
  display: flex;
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 3px;
}

.btn-view-mode {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-family: var(--font-family);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s;
}

.btn-view-mode.active {
  background: var(--gold-primary);
  color: #ffffff;
}

/* Zoom Controls Pill */
.zoom-controls-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  padding: 4px 10px;
  border-radius: var(--radius-full);
}

.btn-zoom {
  background: transparent;
  border: none;
  color: var(--text-primary);
  width: 28px;
  height: 28px;
  border-radius: var(--radius-full);
  font-family: var(--font-family);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.btn-zoom:hover {
  background: var(--gold-glow);
  color: var(--gold-primary);
}

.zoom-slider {
  width: 90px;
  accent-color: var(--gold-primary);
}

.zoom-badge {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--gold-primary);
  min-width: 45px;
  text-align: center;
}

.zoom-badge-btn {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  color: var(--gold-primary);
  font-family: var(--font-family);
  font-size: 0.8rem;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: all 0.2s;
  min-width: 48px;
  text-align: center;
}

.zoom-badge-btn:hover {
  background: var(--gold-glow);
  border-color: var(--gold-primary);
}

.viewer-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Canvas Viewport */
.zoom-viewport {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  overflow: hidden;
  position: relative;
  height: 648px;
  min-height: 648px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  cursor: grab;
  user-select: none;
  box-sizing: border-box;
}

.zoom-content {
  width: 720px;
  height: 616px;
  transform-origin: center center;
  transition: transform 0.05s ease-out;
  position: relative;
  display: block;
  flex-shrink: 0;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.08);
}

#myCanvas {
  display: block;
  width: 720px;
  height: 616px;
  background: #ffffff;
  border-radius: 4px;
}

/* Interactive Grid Overlay on Canvas */
.grid-overlay-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  pointer-events: auto;
  z-index: 10;
}

.grid-clickable-cell {
  border: 1px dashed transparent;
  cursor: pointer;
  transition: background-color 0.2s, border-color 0.2s;
}

.grid-clickable-cell:hover:not(.center-cell) {
  background-color: rgba(217, 119, 6, 0.08);
  border-color: rgba(217, 119, 6, 0.4);
}

.grid-clickable-cell.selected {
  background-color: rgba(217, 119, 6, 0.15) !important;
  border: 2px solid var(--gold-primary) !important;
}

/* Mobile Slide / Carousel Mode Styles */
.mobile-slide-wrapper {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  box-shadow: var(--shadow-sm);
  min-height: 648px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

.slide-nav-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 10px;
  scrollbar-width: thin;
}

.slide-nav-bar::-webkit-scrollbar {
  height: 4px;
}

.slide-nav-bar::-webkit-scrollbar-thumb {
  background: var(--gold-accent);
  border-radius: 4px;
}

.slide-nav-chip {
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  padding: 6px 14px;
  border-radius: var(--radius-full);
  font-family: var(--font-family);
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: all 0.2s;
}

.slide-nav-chip.active {
  background: var(--gold-primary);
  color: #fff;
  border-color: var(--gold-primary);
  box-shadow: var(--shadow-sm);
}

.slide-carousel-wrapper {
  overflow: hidden;
  position: relative;
  width: 100%;
  margin: 15px 0;
  touch-action: pan-y;
}

.slide-carousel-track {
  display: flex;
  transition: transform 0.35s cubic-bezier(0.25, 1, 0.5, 1);
  width: 100%;
}

.slide-card {
  flex: 0 0 100%;
  width: 100%;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.slide-card-header {
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cung-title-row {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: flex-start;
}

.cung-chi-badge {
  font-size: 0.85rem;
  font-weight: 700;
}

.cung-name-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--gold-primary);
}

.cung-tuantriet-badge {
  background: #000;
  color: #fff;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 700;
}

.cung-meta-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.meta-tag {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  padding: 3px 8px;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
}

.badge-menh {
  background: rgba(217, 119, 6, 0.15);
  border-color: var(--gold-primary);
  color: var(--gold-primary);
  font-weight: 700;
}

.badge-than {
  background: rgba(37, 99, 235, 0.15);
  border-color: #2563eb;
  color: #1d4ed8;
  font-weight: 700;
}

/* Stars Inside Slide Card */
.stars-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.section-title {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
}

.stars-tag-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.star-chip {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 700;
}

.star-chip.chinhtinh {
  border-left-width: 4px;
  background: #fff;
  box-shadow: var(--shadow-sm);
}

.star-chip.underlined {
  text-decoration: underline;
  text-decoration-thickness: 2px;
}

.star-elem-mini {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.bottom-stars-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  padding: 10px;
  border-radius: var(--radius-sm);
}

.col-star-box {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.box-lbl {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-muted);
}

.star-pill {
  font-size: 0.82rem;
  font-weight: 700;
}

.relation-chips-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.rel-chip {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  color: var(--gold-primary);
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.rel-chip:hover {
  background: var(--gold-glow);
  border-color: var(--gold-primary);
}

/* Slide Controls */
.slide-control-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 10px;
}

.slide-ctrl-btn {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 8px 16px;
  border-radius: var(--radius-md);
  font-family: var(--font-family);
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
}

.slide-ctrl-btn:hover {
  border-color: var(--gold-primary);
  color: var(--gold-primary);
}

.slide-indicator {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-muted);
}

/* History Drawer (SQLite Database) */
.history-drawer {
  position: fixed;
  top: 0;
  right: -500px;
  width: 480px;
  max-width: 92vw;
  height: 100vh;
  background: var(--bg-secondary);
  border-left: 2px solid var(--border-color);
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  transition: right 0.3s cubic-bezier(0.25, 1, 0.5, 1);
}

.history-drawer.open {
  right: 0;
}

.drawer-header {
  padding: 16px 20px;
  border-bottom: 2px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-primary);
}

.drawer-header h3 {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--gold-primary);
}

.btn-close-drawer {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.6rem;
  cursor: pointer;
}

.drawer-search-bar {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #ffffff;
}

.filters-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.filter-select {
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  font-family: var(--font-family);
  font-size: 0.78rem;
  outline: none;
}

/* SQLite Storage Capacity Bar */
.storage-estimate-box {
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.storage-lbl-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-secondary);
}

.storage-progress-bg {
  width: 100%;
  height: 6px;
  background: #e2e8f0;
  border-radius: 3px;
  overflow: hidden;
}

.storage-progress-bar {
  height: 100%;
  background: var(--gold-accent);
  transition: width 0.3s;
}

.drawer-actions-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.drawer-content {
  flex: 1;
  overflow-y: auto;
  padding: 15px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg-primary);
}

.history-item-card {
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: var(--shadow-sm);
}

.history-item-card:hover {
  border-color: var(--gold-primary);
  background: #fefce8;
}

.history-item-card.active {
  border: 2px solid var(--gold-primary);
  background: #fffbeb;
}

.h-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.h-name {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--gold-primary);
}

.h-gender {
  font-size: 0.75rem;
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  font-weight: 700;
}

.h-gender.male {
  background: rgba(3, 105, 161, 0.12);
  color: #0369a1;
}

.h-gender.female {
  background: rgba(220, 38, 38, 0.12);
  color: #dc2626;
}

.h-card-meta {
  display: flex;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.h-card-tags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.tag-pill {
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: var(--radius-full);
  background: var(--bg-glass);
  color: var(--text-secondary);
}

.tag-pill.menh {
  background: rgba(217, 119, 6, 0.12);
  color: var(--gold-primary);
}

.tag-pill.tag-research {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fde68a;
  cursor: pointer;
  transition: all 0.2s;
}

.tag-pill.tag-research:hover {
  background: #fde68a;
  border-color: #f59e0b;
  transform: translateY(-1px);
}

.h-card-notes {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-style: italic;
  border-left: 3px solid var(--gold-primary);
  padding-left: 6px;
}

.h-card-actions {
  display: flex;
  gap: 6px;
  margin-top: 4px;
}

.h-act-btn {
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-family: var(--font-family);
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.h-act-btn:hover {
  border-color: var(--gold-primary);
  color: var(--gold-primary);
}

.h-act-btn.del:hover {
  border-color: #dc2626;
  color: #dc2626;
}

/* Drawer Backdrop Overlay (Mờ nền khi mở Drawer Lịch Sử hoặc Chi Tiết Cung) */
.drawer-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 999;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s cubic-bezier(0.16, 1, 0.3, 1), visibility 0.25s;
  pointer-events: none;
}

.drawer-backdrop.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* Cung Inspector Side Drawer (Desktop) */
.cung-inspector-drawer {
  position: fixed;
  top: 0;
  left: -440px;
  width: 420px;
  max-width: 90vw;
  height: 100vh;
  background: #ffffff;
  border-right: 2px solid var(--border-color);
  box-shadow: 8px 0 25px rgba(0, 0, 0, 0.12);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  transition: left 0.3s cubic-bezier(0.25, 1, 0.5, 1);
}

.cung-inspector-drawer.open {
  left: 0;
}

.insp-header {
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-color);
}

.insp-meta {
  font-size: 0.82rem;
  color: var(--text-secondary);
}

.insp-sec {
  margin-bottom: 16px;
}

.insp-sec h4 {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-bottom: 8px;
  letter-spacing: 0.4px;
}

.insp-stars-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.insp-badge {
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
}

.insp-badge.mini {
  font-size: 0.78rem;
}

.insp-rel-btn {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  color: var(--gold-primary);
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  font-family: var(--font-family);
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 4px;
}

.insp-rel-btn:hover, .insp-rel-btn:active {
  background: var(--gold-glow);
  border-color: var(--gold-primary);
  transform: translateX(2px);
}

/* Modal Note & Refined Popups */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 16px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.22s ease, visibility 0.22s;
  pointer-events: none;
}

.modal-overlay.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.modal-card {
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-radius: 16px;
  width: 100%;
  max-width: 520px;
  padding: 22px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.18), 0 1px 3px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  gap: 16px;
  transform: scale(0.92) translateY(8px);
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-overlay.open .modal-card {
  transform: scale(1) translateY(0);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--gold-primary);
}

.modal-textarea {
  width: 100%;
  height: 130px;
  background: var(--bg-primary);
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  padding: 12px;
  font-family: var(--font-family);
  font-size: 0.92rem;
  line-height: 1.5;
  outline: none;
  resize: vertical;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.modal-textarea:focus {
  border-color: var(--gold-primary);
  box-shadow: 0 0 0 3px var(--gold-glow);
  background: #ffffff;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 4px;
}

/* ==========================================================================
   INLINE CUNG DETAIL CARD (Hiển thị ngay dưới lá số, KHÔNG che khuất màn hình)
   ========================================================================== */

.cung-detail-inline-card {
  margin-top: 15px;
  background: var(--bg-secondary);
  border: 1.5px solid var(--border-gold);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  box-shadow: 0 4px 15px rgba(217, 119, 6, 0.1);
  display: flex;
  flex-direction: column;
  gap: 14px;
  animation: slideDownFade 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes slideDownFade {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.cd-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  border-bottom: 1.5px solid var(--border-color);
  padding-bottom: 10px;
}

.cd-title-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.cd-cung-badge {
  font-size: 0.85rem;
  font-weight: 800;
  letter-spacing: 0.5px;
}

.cd-cung-name {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--gold-primary);
}

.cd-meta-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.btn-close-inline {
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  width: 28px;
  height: 28px;
  border-radius: var(--radius-full);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  cursor: pointer;
  transition: all 0.2s;
  line-height: 1;
}

.btn-close-inline:hover {
  background: #fee2e2;
  border-color: #dc2626;
  color: #dc2626;
}

.cd-card-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.cd-sec {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cd-sec-lbl {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.cd-stars-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.cd-bottom-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
}

.cd-box {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.cd-box-lbl {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-muted);
}

.cd-box-val {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-primary);
}

.cd-relations-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.cd-rel-btn {
  background: var(--bg-glass);
  border: 1px solid var(--border-color);
  color: var(--gold-primary);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  font-family: var(--font-family);
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.cd-rel-btn:hover {
  background: var(--gold-glow);
  border-color: var(--gold-primary);
  transform: translateY(-1px);
}

/* ==========================================================================
   RESEARCH NOTES & CATEGORIZATION TAGS PANEL
   ========================================================================== */

.research-panel-card {
  margin-top: 20px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.research-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  border-bottom: 1.5px solid var(--border-color);
  padding-bottom: 12px;
}

.research-title {
  display: flex;
  align-items: center;
  gap: 10px;
}

.research-icon {
  font-size: 1.5rem;
}

.research-title h3 {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--gold-primary);
}

.research-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.research-tags-section, .research-notes-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.research-lbl {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.active-tags-container {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-height: 28px;
}

.active-tag-chip {
  background: #fef3c7;
  border: 1px solid #f59e0b;
  color: #92400e;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-size: 0.8rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s;
}

.tag-del-btn {
  background: transparent;
  border: none;
  color: #b45309;
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
  padding: 0 2px;
}

.tag-del-btn:hover {
  color: #dc2626;
}

.add-tag-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.tag-input {
  max-width: 420px;
  padding: 6px 10px;
  font-size: 0.84rem;
}

.suggested-tags-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 4px;
}

.suggest-lbl {
  font-size: 0.74rem;
  color: var(--text-muted);
  font-style: italic;
  padding-top: 4px;
}

.suggest-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  flex: 1;
}

.tag-suggest-btn {
  background: var(--bg-glass);
  border: 1px dashed var(--border-color);
  color: var(--text-secondary);
  padding: 3px 8px;
  border-radius: var(--radius-sm);
  font-family: var(--font-family);
  font-size: 0.74rem;
  cursor: pointer;
  transition: all 0.2s;
}

.tag-suggest-btn:hover {
  border-color: var(--gold-primary);
  background: var(--gold-glow);
  color: var(--gold-primary);
}

.research-textarea {
  width: 100%;
  height: 120px;
  background: #ffffff;
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  padding: 12px;
  font-family: var(--font-family);
  font-size: 0.9rem;
  line-height: 1.5;
  outline: none;
  resize: vertical;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.research-textarea:focus {
  border-color: var(--gold-primary);
  box-shadow: 0 0 0 3px var(--gold-glow);
}

.research-save-status {
  font-size: 0.82rem;
  font-weight: 600;
  min-height: 18px;
  transition: opacity 0.3s;
}

.research-save-status.success {
  color: #16a34a;
}

/* ==========================================================================
   MOBILE & RESPONSIVE TWEAKS (Ergonomic Touch & Drawer Handling)
   ========================================================================== */

@media (max-width: 768px) {
  /* Biến Cung Inspector thành Bottom Sheet trên Mobile để không che tràn cạnh */
  .cung-inspector-drawer {
    top: auto;
    bottom: -100%;
    left: 0;
    right: 0;
    width: 100%;
    max-width: 100%;
    height: 82vh;
    max-height: 85vh;
    border-radius: 20px 20px 0 0;
    border-right: none;
    border-top: 2px solid var(--border-color);
    box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.2);
    transition: bottom 0.35s cubic-bezier(0.25, 1, 0.5, 1);
  }

  .cung-inspector-drawer::before {
    content: '';
    display: block;
    width: 44px;
    height: 5px;
    background: #cbd5e1;
    border-radius: 999px;
    margin: 8px auto 4px;
    flex-shrink: 0;
  }

  .cung-inspector-drawer.open {
    bottom: 0;
    left: 0;
  }

  /* History Drawer mở toàn màn hình trên Mobile để dễ thao tác */
  .history-drawer {
    width: 100vw;
    max-width: 100vw;
    right: -100%;
    border-left: none;
  }

  .history-drawer.open {
    right: 0;
  }

  .drawer-header {
    padding: 12px 16px;
  }

  .drawer-content {
    padding: 12px 14px 28px;
    -webkit-overflow-scrolling: touch;
  }

  /* Form & Viewport */
  .main-wrapper {
    margin: 10px auto;
    padding: 0 10px;
    gap: 14px;
  }

  .form-panel-card {
    padding: 16px 14px;
    border-radius: var(--radius-md);
  }

  /* Toolbar tối ưu cho màn hình cảm ứng */
  .viewer-toolbar {
    padding: 10px;
    gap: 8px;
    justify-content: center;
  }

  .view-mode-toggle {
    width: 100%;
  }

  .btn-view-mode {
    flex: 1;
    justify-content: center;
    text-align: center;
    padding: 8px 10px;
  }

  .zoom-controls-pill {
    flex: 1 1 auto;
    justify-content: space-between;
  }

  .viewer-actions {
    display: flex;
    gap: 6px;
  }

  .input-text, .select-custom {
    min-height: 42px;
    font-size: 0.88rem;
  }

  .btn-submit-primary {
    min-height: 46px;
  }
}

@media (max-width: 480px) {
  .app-header {
    padding: 8px 10px;
  }

  .brand-logo-badge {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }

  .brand-title h1 {
    font-size: 0.95rem;
  }

  .brand-title .brand-sub {
    font-size: 0.65rem;
    max-width: 140px;
  }

  .btn-header {
    padding: 5px 9px;
    font-size: 0.78rem;
    gap: 4px;
  }

  .form-row-2 {
    gap: 8px;
  }

  .form-row-3 {
    grid-template-columns: 0.85fr 1.35fr 1.15fr;
    gap: 4px;
  }

  .form-row-3 .select-custom {
    padding: 8px 4px;
    font-size: 0.82rem;
  }

  .modal-card {
    padding: 16px;
    border-radius: 14px;
  }

  .modal-actions button {
    flex: 1;
    justify-content: center;
  }
}

/* Footer */
.app-footer {
  background: #ffffff;
  border-top: 1px solid var(--border-color);
  padding: 15px 20px;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: auto;
}

.footer-author-link {
  color: var(--gold-primary);
  font-weight: 700;
  text-decoration: none;
  transition: color 0.2s, text-decoration 0.2s;
}

.footer-author-link:hover {
  color: #b45309;
  text-decoration: underline;
}

/* Print Stylesheet */
@media print {
  .app-header, .form-panel-card, .viewer-toolbar, .history-drawer, .cung-inspector-drawer, .app-footer, .zoom-controls-pill {
    display: none !important;
  }
  body, .main-wrapper, .viewer-section, .zoom-viewport {
    background: #ffffff !important;
    color: #000000 !important;
    margin: 0 !important;
    padding: 0 !important;
    width: 100% !important;
    box-shadow: none !important;
  }
  .zoom-content {
    transform: none !important;
  }
  #myCanvas {
    width: 100% !important;
    height: auto !important;
  }
}
