/* ==========================================================================
   main.css - Global theme, layout, variables, and typography
   Default: Clean White / Light Studio Theme (Illustrator & draw.io style)
   ========================================================================== */

:root {
  --bg-app: #f1f5f9;
  --bg-panel: #ffffff;
  --bg-panel-subtle: #f8fafc;
  --bg-panel-hover: #f1f5f9;
  --bg-input: #ffffff;
  
  --border-subtle: #e2e8f0;
  --border-focus: #0284c7;
  --border-active: #0369a1;

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;

  --accent-primary: #0284c7;
  --accent-primary-hover: #0369a1;
  --accent-cyan: #0284c7;
  --accent-emerald: #10b981;
  --accent-rose: #f43f5e;
  --accent-purple: #8b5cf6;

  --grid-dot-color: rgba(15, 23, 42, 0.14);
  --grid-line-color: rgba(15, 23, 42, 0.06);

  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --topbar-height: 48px;
  --toolbar-width: 62px;
  --property-panel-width: 300px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
}

body, html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-app);
  color: var(--text-primary);
  font-family: var(--font-family);
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

/* App Shell Grid */
#app-container {
  display: grid;
  grid-template-rows: var(--topbar-height) 1fr;
  grid-template-columns: var(--toolbar-width) 1fr var(--property-panel-width);
  grid-template-areas:
    "topbar topbar topbar"
    "toolbar canvas properties";
  width: 100vw;
  height: 100vh;
  position: relative;
  overflow: hidden;
}

/* Topbar */
#topbar-container {
  grid-area: topbar;
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px;
  z-index: 50;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

.topbar-left, .topbar-center, .topbar-right {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-right: 6px;
}

.brand-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-title {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.3px;
  color: var(--text-primary);
}

.brand-subtitle {
  font-size: 11px;
  font-weight: 600;
  color: var(--accent-primary);
  background: rgba(2, 132, 199, 0.08);
  padding: 2px 5px;
  border-radius: 4px;
}

.topbar-divider {
  width: 1px;
  height: 18px;
  background-color: var(--border-subtle);
  margin: 0 2px;
}

.topbar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 28px;
  padding: 0 6px;
  background-color: transparent;
  color: var(--text-secondary);
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.topbar-btn:hover:not(:disabled) {
  background-color: var(--bg-panel-hover);
  color: var(--text-primary);
}

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

.topbar-btn.active,
.topbar-btn.secondary.active {
  background-color: var(--accent-primary, #0284c7) !important;
  color: #ffffff !important;
  border-color: var(--accent-primary, #0284c7) !important;
  font-weight: 600;
  box-shadow: 0 1px 3px rgba(2, 132, 199, 0.3);
}

.topbar-btn.primary {
  background-color: var(--accent-primary);
  color: #ffffff;
  padding: 0 10px;
}

.topbar-btn.primary:hover {
  background-color: var(--accent-primary-hover);
}

.topbar-btn.secondary {
  background-color: var(--bg-panel);
  border-color: var(--border-subtle);
  color: var(--text-secondary);
  padding: 0 7px;
}

.topbar-btn.secondary:hover:not(.active) {
  background-color: var(--bg-panel-hover);
  color: var(--text-primary);
}

.topbar-btn-group {
  display: flex;
  align-items: center;
  background-color: var(--bg-panel-subtle);
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  padding: 2px;
}

.topbar-btn-group .topbar-btn {
  border-radius: 4px;
  height: 24px;
  padding: 0 5px;
}

.zoom-text {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  min-width: 38px;
  text-align: center;
  cursor: pointer;
  padding: 3px 4px;
  border-radius: 4px;
}

.zoom-text:hover {
  background-color: var(--bg-panel-hover);
  color: var(--text-primary);
}

/* Page Size Badge / Select */
.page-select-badge {
  display: flex;
  align-items: center;
  gap: 3px;
  background: var(--bg-panel-subtle);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  padding: 2px 5px;
  height: 28px;
}

.page-select-badge select {
  background: transparent;
  border: none;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-primary);
  outline: none;
  cursor: pointer;
  width: 48px;
}

/* Document Title Display */
.doc-title-container {
  display: flex;
  align-items: center;
  padding: 2px 6px;
  background: var(--bg-panel-subtle);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  max-width: 130px;
}

.doc-title {
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-title.dirty {
  color: #0284c7;
  font-weight: 600;
}

/* Grid Unit Badge */
.grid-unit-badge {
  display: flex;
  align-items: center;
  background: var(--bg-panel-subtle);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  padding: 2px 4px;
  height: 28px;
}

.grid-unit-badge select {
  background: transparent;
  border: none;
  font-size: 11px;
  font-weight: 700;
  color: var(--color-primary);
  outline: none;
  cursor: pointer;
}

/* Grid Size Badge */
.grid-size-badge {
  display: flex;
  align-items: center;
  gap: 3px;
  background: var(--bg-panel-subtle);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  padding: 2px 5px;
  height: 28px;
}

.grid-size-badge select {
  background: transparent;
  border: none;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-primary);
  outline: none;
  cursor: pointer;
  width: 48px;
}

/* Toast Notifications */
.sketchy-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background-color: #0f172a;
  color: #f8fafc;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid #334155;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3);
  font-size: 13px;
  font-weight: 500;
  pointer-events: none;
  opacity: 0;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  z-index: 1000;
}

.sketchy-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Dropdown Menu */
.dropdown-wrapper {
  position: relative;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  background-color: var(--bg-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.15);
  min-width: 200px;
  padding: 4px;
  z-index: 100;
}

/* Invisible bridge covering the 4px gap so cursor never drops hover when moving downwards */
.dropdown-menu::before {
  content: '';
  position: absolute;
  top: -12px;
  left: 0;
  right: 0;
  height: 12px;
  pointer-events: auto;
}

.dropdown-menu.align-left {
  left: 0;
  right: auto;
}

.dropdown-wrapper:hover .dropdown-menu,
.dropdown-wrapper.open .dropdown-menu {
  display: block;
}

.dropdown-wrapper.open > .dropdown-btn {
  background-color: var(--bg-panel-hover);
  color: var(--text-primary);
}

.dropdown-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  font-size: 12px;
  color: var(--text-secondary);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.1s ease;
}

.dropdown-item .shortcut {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 16px;
}

.dropdown-item:hover {
  background-color: var(--accent-primary);
  color: #ffffff;
}

.dropdown-item:hover .shortcut {
  color: rgba(255, 255, 255, 0.85);
}

.dropdown-divider {
  height: 1px;
  background-color: var(--border-subtle);
  margin: 4px 0;
}

/* Canvas Area */
#canvas-viewport {
  grid-area: canvas;
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #e2e8f0; /* Studio canvas workspace surrounding white A4 page */
}

#canvas-svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* Context Menu */
#context-menu {
  display: none;
  position: fixed;
  background-color: var(--bg-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.15);
  padding: 4px;
  min-width: 180px;
  z-index: 200;
}

#context-menu.visible {
  display: block;
}

.context-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 7px 12px;
  font-size: 12px;
  color: var(--text-primary);
  border-radius: 4px;
  cursor: pointer;
}

.context-item:hover {
  background-color: var(--accent-primary);
  color: #ffffff;
}

.context-item.disabled {
  opacity: 0.35;
  pointer-events: none;
}

.context-divider {
  height: 1px;
  background-color: var(--border-subtle);
  margin: 4px 0;
}

.shortcut {
  font-size: 10px;
  color: var(--text-muted);
}

/* ==========================================================================
   Preferences Modal
   ========================================================================== */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 99999;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.15s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-card {
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 12px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.05);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: modalSlide 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalSlide {
  from { transform: translateY(12px) scale(0.98); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

.modal-header {
  padding: 14px 20px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  border-bottom: 1px solid #334155;
}

.modal-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: #f8fafc;
  display: flex;
  align-items: center;
  gap: 8px;
}

.modal-subtitle {
  margin: 4px 0 0 0;
  font-size: 12px;
  color: #94a3b8;
}

.modal-close-btn {
  background: transparent;
  border: none;
  color: #94a3b8;
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
  transition: all 0.15s;
}

.modal-close-btn:hover {
  background: #334155;
  color: #ffffff;
}

.modal-body {
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: 82vh;
  overflow-y: auto;
}

.prefs-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.prefs-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #94a3b8;
}

.prefs-desc {
  margin: 0;
  font-size: 12px;
  color: #cbd5e1;
}

.prefs-unit-segmented {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.prefs-unit-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 12px;
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 8px;
  color: #cbd5e1;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.prefs-unit-btn:hover {
  border-color: #0284c7;
  color: #ffffff;
}

.prefs-unit-btn.active {
  background: #0284c7;
  border-color: #38bdf8;
  color: #ffffff;
  box-shadow: 0 4px 12px rgba(2, 132, 199, 0.35);
}

.prefs-grid-styles {
  display: flex;
  gap: 8px;
}

.prefs-style-btn {
  flex: 1;
  padding: 8px 10px;
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 6px;
  color: #cbd5e1;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
}

.prefs-style-btn:hover {
  border-color: #0284c7;
  color: #ffffff;
}

.prefs-style-btn.active {
  background: #1e293b;
  border-color: #0284c7;
  color: #38bdf8;
  font-weight: 700;
}

.prefs-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.prefs-badge {
  font-size: 12px;
  font-weight: 700;
  color: #38bdf8;
  background: #0f172a;
  padding: 2px 8px;
  border-radius: 4px;
  border: 1px solid #334155;
}

.prefs-checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #cbd5e1;
  cursor: pointer;
  user-select: none;
}

.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid #334155;
  display: flex;
  justify-content: flex-end;
  background: rgba(15, 23, 42, 0.4);
}

/* Compact Topbar action buttons by default */
#auto-arrange-label,
#btn-record-steps-label,
.topbar-shortcut-badge,
.snap-label,
#btn-fit-page .topbar-btn-text,
#orientation-label {
  display: none;
}

#btn-mermaid-sync,
#btn-export-dropdown {
  flex-shrink: 0;
}

/* Responsive Topbar adjustments for narrower screens */
@media (max-width: 1480px) {
  #btn-presentation-mode .topbar-btn-text {
    display: none;
  }
  .doc-title-container {
    max-width: 95px;
  }
  .topbar-left, .topbar-center, .topbar-right {
    gap: 4px;
  }
  .topbar-btn {
    padding: 0 5px;
  }
  .brand-subtitle {
    display: none;
  }
}

@media (max-width: 1300px) {
  #btn-export-dropdown > span {
    display: none;
  }
  .doc-title-container {
    max-width: 75px;
  }
  .topbar-left, .topbar-center, .topbar-right {
    gap: 3px;
  }
  .topbar-btn {
    padding: 0 4px;
  }
}

@media (max-width: 1220px) {
  #btn-mermaid-sync span {
    display: none;
  }
}

/* Ultra-wide monitors (>= 1850px): expand descriptive text labels */
@media (min-width: 1850px) {
  #auto-arrange-label,
  #btn-record-steps-label,
  .snap-label,
  #btn-fit-page .topbar-btn-text,
  #orientation-label,
  .topbar-shortcut-badge {
    display: inline;
  }
}
