:root {
  --bg-color: #f8fafc;
  --bg-gradient: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  --card-bg: rgba(255, 255, 255, 0.7);
  --card-border: rgba(255, 255, 255, 0.4);
  --text-main: #0f172a;
  --text-muted: #64748b;
  --accent-blue: #2563eb;
  --accent-blue-hover: #1d4ed8;
  --accent-green: #059669;
  --accent-red: #dc2626;
  --sidebar-bg: rgba(255, 255, 255, 0.85);
  --table-header-bg: rgba(241, 245, 249, 0.8);
  --table-hover-bg: rgba(241, 245, 249, 0.6);
  --input-bg: rgba(255, 255, 255, 0.9);
  --glass-blur: blur(16px);
  --font-family: 'Inter', system-ui, sans-serif;
  --sidebar-width: 260px;
}

[data-theme="dark"] {
  --bg-color: #0f172a;
  --bg-gradient: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
  --card-bg: rgba(30, 41, 59, 0.7);
  --card-border: rgba(255, 255, 255, 0.1);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --accent-blue: #3b82f6;
  --accent-blue-hover: #60a5fa;
  --accent-green: #10b981;
  --accent-red: #ef4444;
  --sidebar-bg: rgba(15, 23, 42, 0.8);
  --table-header-bg: rgba(15, 23, 42, 0.6);
  --table-hover-bg: rgba(51, 65, 85, 0.5);
  --input-bg: rgba(30, 41, 59, 0.8);
}

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

body {
  font-family: var(--font-family);
  background: var(--bg-gradient);
  color: var(--text-main);
  min-height: 100vh;
  display: block;
  overflow-x: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  backdrop-filter: var(--glass-blur);
  border-right: 1px solid var(--card-border);
  display: flex;
  flex-direction: column;
  padding: 2rem 1rem;
  position: fixed;
  height: 100vh;
  z-index: 50;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.brand {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 3rem;
  color: var(--accent-blue);
  text-align: center;
  letter-spacing: 1px;
}

.nav-links {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.nav-item {
  padding: 1rem 1.5rem;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-muted);
}

.nav-item:hover, .nav-item.active {
  background: rgba(59, 130, 246, 0.1);
  color: var(--accent-blue);
}

.nav-item i {
  font-size: 1.2rem;
}

/* Main Content */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  padding: 2rem;
  min-height: 100vh;
}

.mobile-header {
  display: none;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

h1 {
  font-size: 2rem;
  font-weight: 700;
}

.glass-card {
  background: var(--card-bg);
  backdrop-filter: var(--glass-blur);
  border: 1px solid var(--card-border);
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.glass-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

/* Dashboard Grid */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.metric-card {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.metric-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.2);
}

.metric-title {
  font-size: 0.875rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.metric-value {
  font-size: 1.875rem;
  font-weight: 700;
}

.positive { color: var(--accent-green); }
.negative { color: var(--accent-red); }

/* Table Controls (Filters/Search) */
.table-controls {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}
.table-controls input, .table-controls select {
  max-width: 250px;
  padding: 0.6rem 1rem;
}

/* Tables */
.table-container {
  overflow-x: auto;
  border-radius: 12px;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  white-space: nowrap;
}

th, td {
  padding: 1rem;
  border-bottom: 1px solid var(--card-border);
}

th {
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.05em;
  background: var(--table-header-bg);
}

th.sortable {
  cursor: pointer;
  user-select: none;
  transition: color 0.2s;
}
th.sortable:hover {
  color: var(--accent-blue);
}
th.sortable i {
  margin-left: 0.25rem;
  opacity: 0.4;
  transition: opacity 0.2s;
}
th.sortable:hover i, th.sortable.active i {
  opacity: 1;
}

tbody tr {
  transition: background 0.2s;
}

tbody tr:hover {
  background: var(--table-hover-bg);
}

.badge {
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge.open-buy { background: rgba(16, 185, 129, 0.2); color: var(--accent-green); }
.badge.open-sell { background: rgba(239, 68, 68, 0.2); color: var(--accent-red); }
.badge.closed { background: rgba(148, 163, 184, 0.2); color: var(--text-muted); }

/* Forms & Buttons */
.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: var(--text-muted);
  font-size: 0.875rem;
}

input, select, textarea {
  width: 100%;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--card-border);
  background: var(--input-bg);
  color: var(--text-main);
  font-family: inherit;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  resize: vertical;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent-blue);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.btn {
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  border: none;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  font-family: inherit;
  font-size: 1rem;
}

.btn-primary {
  background: var(--accent-blue);
  color: white;
  box-shadow: 0 4px 6px -1px rgba(59, 130, 246, 0.4);
}

.btn-primary:hover {
  background: var(--accent-blue-hover);
  transform: translateY(-2px);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.form-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
  width: 100%;
}
.form-actions .btn {
  flex: 1;
  white-space: nowrap;
}

/* Sections */
.section {
  display: none;
  animation: fadeIn 0.4s ease-out;
}

.section.active {
  display: block;
}

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

/* Topbar for mobile */
.mobile-header {
  display: none;
  padding: 1rem 1.5rem;
  background: var(--sidebar-bg);
  backdrop-filter: var(--glass-blur);
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--card-border);
  position: sticky;
  top: 0;
  z-index: 40;
}

.menu-toggle {
  background: none;
  border: none;
  color: var(--text-main);
  font-size: 1.5rem;
  cursor: pointer;
}

.logic-section p {
  line-height: 1.6;
  margin-bottom: 1rem;
  color: var(--text-muted);
}
.logic-section h3 {
  margin-top: 1.5rem;
  margin-bottom: 0.5rem;
  color: var(--accent-blue);
}
.logic-section ul {
  margin-left: 1.5rem;
  margin-bottom: 1rem;
  color: var(--text-muted);
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--card-border);
  border-left-color: var(--accent-blue);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 40px auto;
}

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

.skeleton {
  background: var(--card-border);
  border-radius: 4px;
  animation: pulse 1.5s infinite ease-in-out;
}
.skeleton-text {
  height: 2rem;
  width: 70%;
  margin-top: 0.5rem;
}

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.4; }
  100% { opacity: 1; }
}

/* Toast Notifications */
.toast-container {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}
.toast {
  background: var(--card-bg);
  backdrop-filter: var(--glass-blur);
  border-left: 4px solid var(--accent-red);
  padding: 1rem 1.5rem;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  animation: slideInRight 0.3s ease-out forwards;
  max-width: 350px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  border-top: 1px solid var(--card-border);
  border-right: 1px solid var(--card-border);
  border-bottom: 1px solid var(--card-border);
}
.toast-title {
  font-weight: 700;
  color: var(--accent-red);
  font-size: 0.9rem;
}
.toast-message {
  font-size: 0.8rem;
  color: var(--text-main);
  word-break: break-word;
}
@keyframes slideInRight {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Responsive */
@media (max-width: 768px) {
  .sidebar {
    transform: none;
    width: 100%;
    height: 75px;
    bottom: 0;
    top: auto;
    flex-direction: row;
    padding: 0;
    border-right: none;
    border-top: 1px solid var(--card-border);
    justify-content: space-around;
    z-index: 50;
    box-shadow: 0 -4px 10px rgba(0,0,0,0.05);
  }
  .sidebar .brand,
  .desktop-profile {
    display: none !important;
  }
  .nav-links {
    flex-direction: row;
    width: 100%;
    justify-content: space-around;
    align-items: center;
    padding: 0;
  }
  .nav-item {
    flex-direction: column;
    padding: 0.5rem 0.25rem;
    gap: 4px;
    font-size: 0.65rem;
    border-radius: 12px;
    flex: 1;
    text-align: center;
    justify-content: center;
    margin: 6px 4px;
    font-weight: 600;
  }
  .nav-item.active {
    background: rgba(59, 130, 246, 0.15);
  }
  .nav-item i {
    font-size: 1.25rem;
    margin-bottom: 2px;
  }
  .theme-toggle-container {
    display: none !important;
  }
  .menu-toggle {
    display: none;
  }
  .main-content {
    margin-left: 0;
    padding: 1rem;
    padding-top: 80px;
    padding-bottom: 100px;
  }
  .mobile-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    background: var(--sidebar-bg);
    backdrop-filter: var(--glass-blur);
    border-bottom: 1px solid var(--card-border);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 50;
  }
  .mobile-header .brand {
    margin-bottom: 0;
    font-size: 1.25rem;
    text-align: left;
  }
  .form-actions {
    flex-direction: column;
    gap: 0.5rem;
  }
  .form-actions .btn {
    width: 100%;
  }
  .form-group {
    margin-bottom: 0.75rem;
  }
  label {
    margin-bottom: 0.25rem;
    font-size: 0.8rem;
  }
  input, select, textarea {
    padding: 0.5rem 0.75rem;
  }
  .form-grid {
    gap: 0.75rem;
  }
  .metrics-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
  }
  .metric-value {
    font-size: 1.25rem;
  }
  .metric-title {
    font-size: 0.75rem;
  }
  .glass-card {
    padding: 1rem;
  }
  h1 {
    font-size: 1.5rem;
  }
  #btn-nav-custom-metrics {
    width: 100%;
    margin-top: 0.5rem;
  }
  .toast-container {
    bottom: 90px;
    right: 1rem;
    left: 1rem;
  }
  .toast {
    max-width: 100%;
  }
}
@media (max-width: 400px) {
  .metrics-grid {
    grid-template-columns: 1fr;
  }
}
