:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-card: #16213e;
  --bg-hover: #1f2b47;
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0b0;
  --text-muted: #6c6c7c;
  --accent: #6c63ff;
  --accent-hover: #5a52d5;
  --border: #2a2a3e;
  --success: #4caf50;
  --danger: #f44336;
  --warning: #ff9800;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --transition: all 0.2s ease;
}

[data-theme="light"] {
  --bg-primary: #f5f5f5;
  --bg-secondary: #ffffff;
  --bg-card: #ffffff;
  --bg-hover: #f0f0f0;
  --text-primary: #1a1a1a;
  --text-secondary: #555555;
  --text-muted: #888888;
  --border: #e0e0e0;
  --shadow: 0 2px 8px rgba(0,0,0,0.1);
}

[data-theme="blue"] {
  --bg-primary: #0a192f;
  --bg-secondary: #112240;
  --bg-card: #1d3461;
  --bg-hover: #233554;
  --accent: #64ffda;
  --accent-hover: #4cd9b5;
}

[data-theme="green"] {
  --bg-primary: #0d1f0d;
  --bg-secondary: #1a2e1a;
  --bg-card: #1e3a1e;
  --bg-hover: #2a4a2a;
  --accent: #66bb6a;
  --accent-hover: #4caf50;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
}

#app {
  display: flex;
  height: 100vh;
}

/* Sidebar */
#sidebar {
  width: 240px;
  min-width: 240px;
  background: var(--bg-secondary);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  transition: width 0.3s ease, min-width 0.3s ease;
  overflow: hidden;
}

#sidebar.collapsed {
  width: 60px;
  min-width: 60px;
}

#sidebar.collapsed .logo-text,
#sidebar.collapsed .menu-text,
#sidebar.collapsed .menu-arrow,
#sidebar.collapsed .submenu {
  display: none;
}

#sidebar.collapsed .sidebar-header {
  justify-content: center;
  padding: 20px 10px;
}

#sidebar.collapsed .menu-item {
  justify-content: center;
  padding: 10px;
}

#sidebar.collapsed .submenu-item {
  display: none;
}

.sidebar-header {
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  position: relative;
}

#sidebar-toggle {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  margin-left: auto;
  padding: 4px 8px;
  border-radius: 4px;
  transition: var(--transition);
}

#sidebar-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.logo { font-size: 24px; }
.logo-text { font-size: 18px; font-weight: 600; }

#menu {
  flex: 1;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius);
  color: var(--text-secondary);
  text-decoration: none;
  transition: var(--transition);
  cursor: pointer;
}

.menu-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.menu-item.active { background: var(--accent); color: #fff; }
.menu-item.active .menu-icon { filter: none; }

.menu-icon { font-size: 18px; width: 24px; text-align: center; }
.menu-text { flex: 1; font-size: 14px; }
.menu-arrow { font-size: 12px; transition: var(--transition); }
.menu-group.open .menu-arrow { transform: rotate(90deg); }

.submenu {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease;
}

.menu-group.open .submenu { max-height: 200px; }

.submenu-item {
  display: block;
  padding: 8px 12px 8px 46px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 13px;
  border-radius: var(--radius);
  transition: var(--transition);
}

.submenu-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.submenu-item.active { color: var(--accent); background: var(--bg-hover); }

.sidebar-footer {
  padding: 12px 8px;
  border-top: 1px solid var(--border);
}

/* Main Content */
#content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#page-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
}

#page-title {
  font-size: 20px;
  font-weight: 600;
}

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

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  transition: var(--transition);
}

.card:hover { border-color: var(--accent); box-shadow: var(--shadow); }

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.card-title { font-weight: 600; font-size: 15px; }
.card-desc { color: var(--text-secondary); font-size: 13px; }

/* Buttons */
.btn {
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--bg-hover); color: var(--text-primary); border: 1px solid var(--border); }
.btn-secondary:hover { border-color: var(--accent); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 4px 10px; font-size: 12px; }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-label { display: block; margin-bottom: 6px; font-size: 13px; color: var(--text-secondary); font-weight: 500; }
.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  transition: var(--transition);
}

.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.form-textarea { resize: vertical; min-height: 100px; font-family: inherit; }

/* Table */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: 12px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { font-size: 12px; color: var(--text-muted); text-transform: uppercase; font-weight: 600; }
.table tr:hover { background: var(--bg-hover); }

/* Badge */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
}
.badge-success { background: rgba(76,175,80,0.2); color: var(--success); }
.badge-warning { background: rgba(255,152,0,0.2); color: var(--warning); }
.badge-danger { background: rgba(244,67,54,0.2); color: var(--danger); }

/* Chat */
.chat-container { display: flex; flex-direction: column; height: calc(100vh - 140px); }
.chat-messages { flex: 1; overflow-y: auto; padding: 16px 0; }
.chat-message { margin-bottom: 16px; display: flex; gap: 12px; }
.chat-message.user { flex-direction: row-reverse; }
.chat-avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--accent); display: flex; align-items: center; justify-content: center; font-size: 14px; flex-shrink: 0; }
.chat-bubble { max-width: 70%; padding: 12px 16px; border-radius: 12px; font-size: 14px; line-height: 1.5; }
.chat-message.user .chat-bubble { background: var(--accent); color: #fff; }
.chat-message.assistant .chat-bubble { background: var(--bg-card); border: 1px solid var(--border); }
.chat-input-area { display: flex; gap: 8px; padding-top: 16px; border-top: 1px solid var(--border); }
.chat-input { flex: 1; padding: 12px 16px; background: var(--bg-card); border: 1px solid var(--border); border-radius: 24px; color: var(--text-primary); font-size: 14px; resize: none; }
.chat-input:focus { outline: none; border-color: var(--accent); }

/* Workspace */
.workspace-container { display: flex; gap: 16px; height: calc(100vh - 140px); }
.file-tree { width: 280px; min-width: 280px; overflow-y: auto; background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.file-preview { flex: 1; background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column; }
.file-preview-header { padding: 12px 16px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.file-preview-body { flex: 1; overflow: auto; padding: 16px; }
.file-preview-body pre { white-space: pre-wrap; word-break: break-all; font-size: 13px; line-height: 1.6; font-family: 'Fira Code', monospace; }
.file-preview-body textarea { width: 100%; height: 100%; background: transparent; border: none; color: var(--text-primary); font-family: 'Fira Code', monospace; font-size: 13px; line-height: 1.6; resize: none; }

.tree-item { display: flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 4px; cursor: pointer; font-size: 13px; color: var(--text-secondary); transition: var(--transition); }
.tree-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.tree-item.active { background: var(--accent); color: #fff; }
.tree-item .icon { width: 16px; text-align: center; }
.tree-children { padding-left: 16px; display: none; }
.tree-folder.open > .tree-children { display: block; }
.tree-arrow { font-size: 10px; width: 14px; text-align: center; transition: transform 0.2s; color: var(--text-muted, #666); flex-shrink: 0; }
.tree-folder.open > .tree-item > .tree-arrow { transform: rotate(90deg); }
.tree-arrow-placeholder { width: 14px; flex-shrink: 0; }

/* Toolbar */
.toolbar { display: flex; gap: 8px; margin-bottom: 16px; align-items: center; }
.toolbar-right { margin-left: auto; display: flex; gap: 8px; }

/* Tabs */
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tab { padding: 10px 16px; cursor: pointer; color: var(--text-secondary); font-size: 13px; border-bottom: 2px solid transparent; transition: var(--transition); }
.tab:hover { color: var(--text-primary); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* Toast */
#toast-container { position: fixed; top: 20px; right: 20px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; }
.toast { padding: 12px 20px; border-radius: var(--radius); color: #fff; font-size: 13px; animation: slideIn 0.3s ease; box-shadow: var(--shadow); }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
.toast-info { background: var(--accent); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

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

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

.file-tree.refreshing {
  opacity: 0.5;
  position: relative;
}

.file-tree.refreshing::after {
  content: '🔄';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 24px;
  animation: spin 0.8s linear infinite;
}

.file-tree:not(.refreshing) > .tree-folder,
.file-tree:not(.refreshing) > .tree-item {
  animation: fadeIn 0.3s ease;
}

/* Modal */
#modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: flex; align-items: center; justify-content: center; z-index: 1000; }

.datetime-picker { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.datetime-row { display: flex; align-items: center; gap: 4px; }
.datetime-row label { font-size: 12px; color: var(--text-muted); min-width: 20px; }
.dt-field { width: 72px !important; padding: 6px 4px !important; font-size: 13px; text-align: center; }
.form-hint { font-size: 12px; color: var(--text-muted); margin-top: 6px; }
#modal-overlay.hidden { display: none; }
#modal { background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 12px; width: 90%; max-width: 560px; max-height: 80vh; overflow-y: auto; }
#modal-header { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
#modal-title { font-size: 16px; font-weight: 600; }
#modal-close { background: none; border: none; color: var(--text-secondary); font-size: 20px; cursor: pointer; }
#modal-body { padding: 20px; }

/* Grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }

/* User page */
.user-card { text-align: center; padding: 40px; }
.user-avatar { font-size: 64px; margin-bottom: 16px; }
.user-name { font-size: 24px; font-weight: 600; margin-bottom: 8px; }
.user-info { color: var(--text-secondary); font-size: 14px; margin-bottom: 4px; }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* Empty state */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state .icon { font-size: 48px; margin-bottom: 16px; }
.empty-state p { font-size: 14px; }

/* ========== Responsive Design ========== */

/* Tablet (768px - 1024px) */
@media (max-width: 1024px) and (min-width: 769px) {
  #sidebar {
    width: 60px;
    min-width: 60px;
  }
  
  #sidebar .logo-text,
  #sidebar .menu-text,
  #sidebar .menu-arrow,
  #sidebar .submenu {
    display: none;
  }
  
  #sidebar .sidebar-header {
    justify-content: center;
    padding: 20px 10px;
  }
  
  #sidebar .menu-item {
    justify-content: center;
    padding: 10px;
  }
  
  #sidebar.expanded {
    width: 240px;
    min-width: 240px;
  }
  
  #sidebar.expanded .logo-text,
  #sidebar.expanded .menu-text,
  #sidebar.expanded .menu-arrow {
    display: block;
  }
  
  #sidebar.expanded .menu-group.open .submenu {
    display: block;
    max-height: 200px;
  }
  
  #sidebar.expanded .sidebar-header {
    justify-content: flex-start;
    padding: 20px;
  }
  
  #sidebar.expanded .menu-item {
    justify-content: flex-start;
    padding: 10px 12px;
  }
  
  .workspace-container {
    flex-direction: column;
    height: auto;
  }
  
  .file-tree {
    width: 100%;
    min-width: 100%;
    max-height: 300px;
  }
  
  .file-preview {
    min-height: 400px;
  }
  
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}

/* Mobile (< 768px) */
@media (max-width: 768px) {
  #app {
    flex-direction: column;
  }
  
  #sidebar {
    position: fixed;
    left: -240px;
    top: 0;
    bottom: 0;
    width: 240px;
    min-width: 240px;
    z-index: 1000;
    transition: left 0.3s ease;
    box-shadow: 2px 0 10px rgba(0,0,0,0.3);
  }
  
  #sidebar.mobile-open {
    left: 0;
  }
  
  #sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 999;
    display: none;
  }
  
  #sidebar-overlay.active {
    display: block;
  }
  
  #content {
    width: 100%;
    height: 100vh;
  }
  
  #page-header {
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
  }
  
  #mobile-menu-btn {
    display: block;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
  }
  
  #page-title {
    font-size: 16px;
  }
  
  #page-content {
    padding: 16px;
  }
  
  .workspace-container {
    flex-direction: column;
    height: auto;
  }
  
  .file-tree {
    width: 100%;
    min-width: 100%;
    max-height: 250px;
  }
  
  .file-preview {
    min-height: 300px;
  }
  
  .grid {
    grid-template-columns: 1fr;
  }
  
  .table {
    font-size: 12px;
  }
  
  .table th,
  .table td {
    padding: 8px 6px;
  }
  
  .chat-bubble {
    max-width: 85%;
  }
  
  .toolbar {
    flex-wrap: wrap;
  }
  
  .toolbar-right {
    width: 100%;
    justify-content: flex-start;
    margin-left: 0;
    margin-top: 8px;
  }
  
  .btn {
    padding: 6px 12px;
    font-size: 12px;
  }
  
  .card {
    padding: 12px;
  }
  
  .form-input,
  .form-select,
  .form-textarea {
    font-size: 16px; /* Prevent zoom on iOS */
  }
  
  #modal {
    width: 95%;
    max-width: none;
  }
}

/* Small mobile (< 480px) */
@media (max-width: 480px) {
  #page-content {
    padding: 12px;
  }
  
  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }
  
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .tab {
    white-space: nowrap;
    padding: 8px 12px;
  }
}

/* Desktop: hide mobile menu button */
#mobile-menu-btn {
  display: none;
}

@media (max-width: 768px) {
  #mobile-menu-btn {
    display: block;
  }
  
  #sidebar-toggle {
    display: none;
  }
}
