/**
 * DBForge Studio - Master Styles & Design System
 * Inspired by GitHub (Light Theme primary, Dark Theme supported) and VS Code IDE.
 */

:root {
  /* GitHub Light Theme Tokens */
  --bg-app: #f6f8fa;
  --bg-surface: #ffffff;
  --bg-surface-secondary: #f6f8fa;
  --bg-surface-hover: #f3f4f6;
  --bg-surface-active: #ebecf0;

  --border-color: #d0d7de;
  --border-muted: #e1e4e8;
  --border-focus: #0969da;

  --text-primary: #1f2328;
  --text-secondary: #656d76;
  --text-tertiary: #8c959f;
  --text-inverse: #ffffff;

  --accent-primary: #0969da;
  --accent-hover: #0860ca;
  --accent-subtle: #ddf4ff;

  --color-success: #1a7f37;
  --color-success-bg: #dafbe1;
  --color-warning: #9a6700;
  --color-warning-bg: #fff8c5;
  --color-danger: #cf222e;
  --color-danger-bg: #ffebe9;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, monospace;

  --shadow-sm: 0 1px 2px rgba(31, 35, 40, 0.08);
  --shadow-md: 0 3px 6px rgba(31, 35, 40, 0.12), 0 1px 2px rgba(31, 35, 40, 0.08);
  --shadow-lg: 0 8px 24px rgba(31, 35, 40, 0.16);
  --shadow-card: 0 2px 8px rgba(0, 0, 0, 0.08), 0 1px 3px rgba(0, 0, 0, 0.04);

  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;
  --radius-xl: 12px;

  --topbar-height: 48px;
  --statusbar-height: 26px;
  --sidebar-width: 280px;
  --right-panel-width: 340px;
}

[data-theme="dark"] {
  /* GitHub Dark Theme Tokens */
  --bg-app: #0d1117;
  --bg-surface: #161b22;
  --bg-surface-secondary: #0d1117;
  --bg-surface-hover: #1f242c;
  --bg-surface-active: #292e36;

  --border-color: #30363d;
  --border-muted: #21262d;
  --border-focus: #2f81f7;

  --text-primary: #e6edf3;
  --text-secondary: #848d97;
  --text-tertiary: #656c76;
  --text-inverse: #0d1117;

  --accent-primary: #2f81f7;
  --accent-hover: #388bfd;
  --accent-subtle: #0c2d6b;

  --color-success: #238636;
  --color-success-bg: #033a16;
  --color-warning: #d29922;
  --color-warning-bg: #3b2300;
  --color-danger: #f85149;
  --color-danger-bg: #490202;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 3px 6px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  --shadow-card: 0 4px 12px rgba(0, 0, 0, 0.4);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  font-family: var(--font-sans);
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-primary);
  background-color: var(--bg-app);
}

/* Master IDE Layout */
#app-root {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

/* --- Top Bar --- */
#top-bar-header {
  height: var(--topbar-height);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 100;
  user-select: none;
}

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

.brand-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 14px;
  color: var(--text-primary);
  text-decoration: none;
  cursor: pointer;
}

.brand-icon-wrap {
  width: 26px;
  height: 26px;
  background: linear-gradient(135deg, #0969da, #8250df);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  box-shadow: 0 1px 3px rgba(9, 105, 218, 0.3);
}

.project-selector {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  background: var(--bg-surface-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.15s ease;
}

.project-selector:hover {
  border-color: var(--accent-primary);
  background: var(--bg-surface-hover);
}

.project-name {
  font-weight: 600;
  font-size: 12px;
  color: var(--text-primary);
}

.save-status-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--text-secondary);
  padding: 2px 6px;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
}
.status-dot.green { background-color: var(--color-success); }
.status-dot.yellow { background-color: var(--color-warning); }
.status-dot.orange { background-color: #e36209; }

@keyframes pulse {
  0% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(1.3); }
  100% { opacity: 1; transform: scale(1); }
}
.pulse { animation: pulse 1.2s infinite ease-in-out; }

/* Buttons & Controls */
.topbar-btn, .btn-secondary {
  height: 28px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid var(--border-color);
  background: var(--bg-surface);
  color: var(--text-primary);
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: all 0.12s ease;
  user-select: none;
}

.topbar-btn:hover:not(:disabled), .btn-secondary:hover:not(:disabled) {
  background: var(--bg-surface-hover);
  border-color: var(--text-secondary);
}

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

.topbar-btn.primary, .btn-primary {
  background: var(--color-success);
  border-color: rgba(27, 31, 36, 0.15);
  color: #fff;
}

.topbar-btn.primary:hover:not(:disabled), .btn-primary:hover:not(:disabled) {
  background: #1a7f37;
  border-color: rgba(27, 31, 36, 0.15);
}

.topbar-btn.ai-btn {
  background: linear-gradient(135deg, rgba(9, 105, 218, 0.1), rgba(130, 80, 223, 0.1));
  border-color: #8250df;
  color: #8250df;
  font-weight: 600;
}

.topbar-btn.ai-btn:hover {
  background: linear-gradient(135deg, rgba(9, 105, 218, 0.2), rgba(130, 80, 223, 0.2));
}

.topbar-btn.icon-only {
  width: 28px;
  padding: 0;
  justify-content: center;
}

.engine-select {
  height: 28px;
  padding: 0 8px;
  font-size: 12px;
  font-weight: 600;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  cursor: pointer;
  outline: none;
}

.engine-select:hover, .engine-select:focus {
  border-color: var(--accent-primary);
}

/* --- Main IDE Body Grid --- */
#main-ide-workspace {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

/* --- Sidebar --- */
#sidebar-panel {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  z-index: 20;
}

.sidebar-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-muted);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-search-box {
  position: relative;
  display: flex;
  align-items: center;
}

.sidebar-search-box input {
  width: 100%;
  height: 28px;
  padding: 0 8px 0 26px;
  font-size: 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-surface-secondary);
  color: var(--text-primary);
  outline: none;
  transition: all 0.15s ease;
}

.sidebar-search-box input:focus {
  border-color: var(--accent-primary);
  background: var(--bg-surface);
  box-shadow: 0 0 0 2px var(--accent-subtle);
}

.search-icon {
  position: absolute;
  left: 8px;
  color: var(--text-tertiary);
  pointer-events: none;
}

.sidebar-tree-container {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.tree-section {
  margin-bottom: 8px;
}

.tree-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 12px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.1s ease;
}

.tree-section-header:hover {
  background: var(--bg-surface-hover);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 6px;
}

.tree-chevron {
  transition: transform 0.15s ease;
}

.tree-section.collapsed .tree-chevron {
  transform: rotate(-90deg);
}

.tree-section.collapsed .tree-section-content {
  display: none;
}

.badge-pill {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--border-muted);
  color: var(--text-secondary);
}

.header-action-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-action-btn:hover {
  color: var(--accent-primary);
  background: var(--bg-surface-secondary);
}

.tree-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px 5px 24px;
  font-size: 12px;
  color: var(--text-primary);
  cursor: pointer;
  transition: background 0.1s ease;
}

.tree-item:hover {
  background: var(--bg-surface-hover);
}

.tree-item.active {
  background: var(--accent-subtle);
  color: var(--accent-primary);
  font-weight: 600;
}

.table-color-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.tree-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-item-count {
  font-size: 10px;
  color: var(--text-tertiary);
}

.tree-empty-msg {
  padding: 8px 24px;
  font-size: 11px;
  font-style: italic;
  color: var(--text-tertiary);
}

.tree-item.rel-item {
  font-size: 11px;
}

.tree-rel-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-rel-type {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 4px;
  border-radius: 4px;
  background: var(--border-muted);
  color: var(--text-secondary);
}

/* --- Right Side Panel --- */
#right-side-panel {
  width: var(--right-panel-width);
  min-width: var(--right-panel-width);
  background: var(--bg-surface);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 20;
}

.right-panel-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-surface-secondary);
}

.right-panel-tab-btn {
  flex: 1;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-bottom: 2px solid transparent;
  transition: all 0.15s ease;
}

.right-panel-tab-btn:hover {
  color: var(--text-primary);
  background: var(--bg-surface);
}

.right-panel-tab-btn.active {
  color: var(--accent-primary);
  background: var(--bg-surface);
  border-bottom-color: var(--accent-primary);
}

.right-panel-content {
  flex: 1;
  overflow-y: auto;
  position: relative;
}

/* Custom Scrollbars */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-tertiary);
}

/* ========================================================== */
/* RESPONSIVE DESIGN: MOBILE & TABLET SYSTEM                  */
/* ========================================================== */

/* Desktop defaults */
.mobile-only {
  display: none !important;
}

#mobile-bottom-nav {
  display: none;
}

#mobile-drawer-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 20, 25, 0.45);
  backdrop-filter: blur(2px);
  z-index: 450;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

#mobile-drawer-backdrop.active {
  opacity: 1;
  pointer-events: all;
}

/* Tablet (max-width: 1024px) */
@media (max-width: 1024px) {
  :root {
    --sidebar-width: 240px;
    --right-panel-width: 300px;
  }

  .topbar-btn {
    height: 38px;
    padding: 0 12px;
    font-size: 11px;
  }

  .topbar-btn svg {
    width: 16px;
    height: 16px;
  }
}

/* Mobile & Android Devices (max-width: 768px) */
@media (max-width: 768px) {
  .mobile-only {
    display: flex !important;
  }

  .desktop-only,
  #btn-import,
  #btn-export,
  #btn-generate-sql,
  #btn-toggle-ai,
  .topbar-center .topbar-btn:not(.primary):not(.icon-only) {
    display: none !important;
  }

  /* Hide brand text to save space on mobile */
  .brand-badge span {
    display: none;
  }

  /* Compact Top Bar with safe-area-inset-top for notch/camera cutouts */
  #top-bar-header {
    padding-top: max(4px, env(safe-area-inset-top, 0px));
    padding-left: max(10px, env(safe-area-inset-left, 0px));
    padding-right: max(10px, env(safe-area-inset-right, 0px));
    padding-bottom: 4px;
    height: calc(48px + env(safe-area-inset-top, 0px));
    box-sizing: border-box;
  }

  .topbar-left {
    gap: 6px;
    flex-shrink: 1;
    overflow: hidden;
  }

  .brand-badge {
    gap: 6px;
  }

  .brand-badge span {
    font-size: 13px;
    font-weight: 700;
  }

  .project-selector {
    padding: 3px 6px;
    background: var(--bg-surface-secondary);
    border-radius: var(--radius-sm);
  }

  .project-name {
    font-size: 11px;
    max-width: 75px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .save-status-badge .status-label {
    display: none;
  }

  .topbar-center {
    gap: 6px;
  }

  .engine-select {
    height: 38px;
    font-size: 12px;
    padding: 0 6px;
    max-width: 90px;
    background: var(--bg-surface-secondary);
  }

  .topbar-right {
    gap: 6px;
  }

  .topbar-btn.primary {
    height: 38px;
    padding: 0 12px;
    font-size: 12px;
  }

  .topbar-btn.ai-btn {
    height: 38px;
    padding: 0 12px;
    font-size: 12px;
  }

  /* Main Workspace occupies dynamic space between topbar and bottom nav */
  #main-ide-workspace {
    flex: 1;
    height: calc(100dvh - (48px + env(safe-area-inset-top, 0px)) - (56px + env(safe-area-inset-bottom, 0px)));
    overflow: hidden;
    position: relative;
  }

  /* Left Sidebar Drawer */
  #sidebar-panel {
    position: fixed;
    top: calc(48px + env(safe-area-inset-top, 0px));
    left: 0;
    bottom: calc(56px + env(safe-area-inset-bottom, 0px));
    width: 85%;
    max-width: 320px;
    background: var(--bg-surface);
    box-shadow: var(--shadow-lg);
    z-index: 500;
    transform: translateX(-100%);
    transition: transform 0.26s cubic-bezier(0.2, 0, 0, 1);
  }

  #sidebar-panel.drawer-open {
    transform: translateX(0);
  }

  /* Right Panel Drawer (Inspector & AI) */
  #right-side-panel {
    position: fixed;
    top: calc(48px + env(safe-area-inset-top, 0px));
    right: 0;
    bottom: calc(56px + env(safe-area-inset-bottom, 0px));
    width: 92%;
    max-width: 380px;
    background: var(--bg-surface);
    box-shadow: var(--shadow-lg);
    z-index: 500;
    transform: translateX(100%);
    transition: transform 0.26s cubic-bezier(0.2, 0, 0, 1);
  }

  #right-side-panel.drawer-open {
    transform: translateX(0);
  }

  /* Bottom Panel Sheet (Live SQL & Console) */
  #bottom-panel-container {
    position: fixed;
    bottom: calc(56px + env(safe-area-inset-bottom, 0px));
    left: 0;
    width: 100%;
    max-height: 72dvh;
    background: var(--bg-surface);
    border-top: 1px solid var(--border-color);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
    z-index: 480;
    transform: translateY(100%);
    transition: transform 0.26s cubic-bezier(0.2, 0, 0, 1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  #bottom-panel-container.sheet-open {
    transform: translateY(0);
    height: 58dvh !important;
  }

  /* Mobile Pull Handle Indicator */
  .mobile-sheet-pull-bar {
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--border-color);
    margin: 8px auto 4px auto;
    flex-shrink: 0;
  }

  /* Mobile Bottom Navigation Bar (Oppo Reno / Android Gesture Safe) */
  #mobile-bottom-nav {
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: calc(56px + env(safe-area-inset-bottom, 0px));
    background: var(--bg-surface);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border-color);
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 600;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    user-select: none;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.04);
  }

  .mobile-nav-btn {
    flex: 1;
    height: 100%;
    min-height: 48px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.2px;
    transition: all 0.15s ease;
    touch-action: manipulation;
    position: relative;
    padding: 4px 0;
  }

  .mobile-nav-btn:active {
    transform: scale(0.92);
  }

  .mobile-nav-btn.active {
    color: var(--accent-primary);
  }

  .mobile-nav-btn.active::before {
    content: '';
    position: absolute;
    top: 2px;
    width: 28px;
    height: 3px;
    border-radius: 3px;
    background: var(--accent-primary);
  }

  .mobile-nav-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    line-height: 1;
  }

  .mobile-nav-label {
    font-size: 10px;
    line-height: 1.1;
  }

  /* Canvas Toolbar on Mobile */
  .canvas-toolbar {
    bottom: 12px;
    max-width: 95%;
    padding: 4px 8px;
    gap: 3px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .canvas-toolbar::-webkit-scrollbar {
    display: none;
  }

  .toolbar-btn {
    height: 32px;
    padding: 0 8px;
    font-size: 11px;
    white-space: nowrap;
    border-radius: var(--radius-sm);
  }

  .minimap-container {
    display: none !important;
  }
}

/* Smartphone portrait specific adjustments (e.g. Oppo Reno 14F, ~360px - 412px) */
@media (max-width: 480px) {
  #top-bar-header {
    padding-left: 8px;
    padding-right: 8px;
  }

  .project-selector {
    max-width: 75px;
    padding: 2px 5px;
  }

  .project-name {
    font-size: 10px;
    max-width: 55px;
  }

  .topbar-center #btn-undo,
  .topbar-center #btn-redo {
    display: none !important;
  }

  .engine-select {
    max-width: 72px;
    font-size: 10px;
    padding: 0 2px;
  }

  .topbar-btn.primary {
    font-size: 0; /* Ocultar texto "Validate" */
    padding: 0;
    width: 38px;
    justify-content: center;
  }

  .topbar-btn.primary svg {
    margin: 0;
  }
}

/* ========================================================== */
/* LANDSCAPE MOBILE / SHORT SCREENS                           */
/* ========================================================== */
@media (max-width: 768px) and (max-height: 500px) {
  #mobile-bottom-nav {
    height: calc(42px + env(safe-area-inset-bottom, 0px));
  }

  .mobile-nav-label {
    display: none;
  }

  .mobile-nav-icon svg {
    width: 18px;
    height: 18px;
  }

  #main-ide-workspace {
    height: calc(100dvh - 48px - 42px);
  }

  #sidebar-panel {
    bottom: calc(42px + env(safe-area-inset-bottom, 0px));
  }

  #right-side-panel {
    bottom: calc(42px + env(safe-area-inset-bottom, 0px));
  }

  #bottom-panel-container {
    bottom: calc(42px + env(safe-area-inset-bottom, 0px));
    max-height: 60dvh;
  }

  #bottom-panel-container.sheet-open {
    height: 50dvh !important;
  }
}

