/* ===== GLOBAL RESET & VARIABLES ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0d0f14;
  --surface: #161923;
  --surface2: #1e2330;
  --border: #2a3045;
  --accent: #4f8ef7;
  --accent2: #7c5cfc;
  --success: #34d399;
  --warning: #f59e0b;
  --danger: #ef4444;
  --text: #e8eaf0;
  --text-muted: #7a8090;
  --text-dim: #4a5060;
  --font: 'Inter', sans-serif;
  --mono: 'JetBrains Mono', monospace;
  --radius: 12px;
  --radius-sm: 8px;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
}

/* ===== LAYOUT ===== */
.app-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.main-layout {
  display: grid;
  grid-template-columns: 280px 1fr 300px;
  gap: 16px;
  align-items: start;
}

/* ===== HEADER ===== */
.header {
  background: linear-gradient(135deg, #1a1f35 0%, #0f1525 100%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 28px;
  margin-bottom: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  /* Allow wrapping on very small screens */
  gap: 20px;
}

.site-link {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 15px;
  font-weight: 600;
  transition: color 0.2s;
  display: flex;
  align-items: center;
  gap: 6px;
}

.site-link::after {
  content: '↗';
  font-size: 13px;
  opacity: 0.7;
}

.site-link:hover {
  color: var(--accent);
}

.header-content {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  font-size: 36px;
  filter: drop-shadow(0 0 16px #4f8ef755);
}

.header-title {
  font-size: 22px;
  font-weight: 700;
  background: linear-gradient(90deg, #4f8ef7, #7c5cfc);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-subtitle {
  color: var(--text-muted);
  font-size: 13px;
  margin-top: 2px;
}

/* ===== PANELS ===== */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.panel-title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text);
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

/* ===== INPUT PANEL ===== */
.section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin-bottom: 10px;
}

.input-group {
  margin-bottom: 14px;
}

.input-group label {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 5px;
  font-weight: 500;
}

.input-wrapper {
  display: flex;
  align-items: center;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  transition: border-color 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px #4f8ef720;
}

.input-wrapper input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-family: var(--mono);
  font-size: 14px;
  padding: 8px 10px;
}

.input-wrapper .unit {
  padding: 0 10px;
  color: var(--text-dim);
  font-size: 12px;
  font-family: var(--mono);
  background: #ffffff08;
  border-left: 1px solid var(--border);
  height: 100%;
  display: flex;
  align-items: center;
  min-width: 42px;
  justify-content: center;
}

.hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 3px;
  display: block;
}

.calc-btn {
  width: 100%;
  margin-top: 18px;
  padding: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border: none;
  border-radius: var(--radius-sm);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s, box-shadow 0.2s;
  box-shadow: 0 4px 16px #4f8ef740;
}

.calc-btn:hover {
  opacity: 0.92;
  box-shadow: 0 6px 24px #4f8ef755;
}

.calc-btn:active {
  transform: scale(0.98);
}

/* ===== CENTER PANEL ===== */
.center-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.canvas-wrapper {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#mechanismCanvas {
  display: block;
  width: 100%;
  height: auto;
}

.angle-control label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  display: block;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.angle-val {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  min-width: 40px;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  border-radius: 4px;
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  box-shadow: 0 0 6px var(--accent);
}

.anim-controls {
  display: flex;
  gap: 8px;
}

.anim-btn {
  flex: 1;
  padding: 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
  font-weight: 500;
}

.anim-btn.active {
  background: linear-gradient(135deg, #1e3a2a, #1a3040);
  border-color: var(--success);
  color: var(--success);
}

.anim-btn:hover {
  border-color: var(--accent);
}

.quick-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.stat-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.stat-label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 3px;
}

.stat-value {
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
}

/* ===== RESULT PANEL ===== */
.result-section {
  margin-bottom: 18px;
}

.result-section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--accent2);
  margin-bottom: 8px;
  padding-bottom: 5px;
  border-bottom: 1px solid var(--border);
}

.result-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 2px;
  border-radius: 4px;
  font-size: 12.5px;
  transition: background 0.15s;
}

.result-row:hover {
  background: var(--surface2);
  padding: 5px 6px;
}

.result-row span:first-child {
  color: var(--text-muted);
}

.result-val {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.result-val.accent {
  color: var(--success);
  font-size: 14px;
}

.highlight-row {
  background: #1a2a1a;
  border-radius: 6px;
  padding: 7px 8px !important;
  margin: 3px 0;
  border: 1px solid #2a3a2a;
}

.formula-box {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent2);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-top: 6px;
}

.formula-title {
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 8px;
  letter-spacing: 0.07em;
}

.formula {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent);
  margin-bottom: 4px;
  line-height: 1.6;
}

/* ===== CHART SECTION ===== */
.chart-section {
  margin-top: 0;
}

.chart-section .panel-title {
  margin-bottom: 4px;
}

#profileChart {
  width: 100%;
  height: auto;
  display: block;
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: var(--bg);
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

/* ===== VALIDATION BANNER ===== */
.validation-banner {
  display: none;
  align-items: flex-start;
  gap: 14px;
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 4px;
  animation: slideDown 0.25s ease;
  border: 1px solid transparent;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.validation-banner.error {
  background: linear-gradient(135deg, #2a1015, #1a0d10);
  border-color: #ef444455;
}

.validation-banner.warning {
  background: linear-gradient(135deg, #2a2010, #1a1508);
  border-color: #f59e0b55;
}

.vb-icon {
  font-size: 22px;
  line-height: 1.3;
  flex-shrink: 0;
}

.vb-messages {
  flex: 1;
}

.vb-msg {
  font-size: 13px;
  line-height: 1.6;
  margin-bottom: 2px;
}

.validation-banner.error .vb-msg {
  color: #fca5a5;
}

.validation-banner.warning .vb-msg {
  color: #fcd34d;
}

/* ===== INPUT ERROR / WARNING STATES ===== */
.input-wrapper.input-error {
  border-color: var(--danger) !important;
  box-shadow: 0 0 0 2px #ef444425;
}

.input-wrapper.input-warning {
  border-color: var(--warning) !important;
  box-shadow: 0 0 0 2px #f59e0b20;
}

/* ===== RESPONSIVE DESIGN (MOBILE/TABLET) ===== */
@media (max-width: 960px) {
  .app-container {
    padding: 10px 10px 30px;
  }

  .main-layout {
    grid-template-columns: 1fr;
    /* Stack all panels vertically */
    gap: 20px;
  }

  /* On mobile, show Visualization FIRST, then Inputs, then Results */
  .center-panel {
    order: 1;
  }

  .input-panel {
    order: 2;
  }

  .result-panel {
    order: 3;
  }

  .header {
    padding: 16px 20px;
    flex-direction: column;
    justify-content: center;
    gap: 16px;
  }

  .header-content {
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }

  .header-title {
    font-size: 20px;
  }

  .input-group label {
    font-size: 13px;
  }

  /* Make inputs for touch */
  .input-wrapper input {
    font-size: 16px;
    /* Prevents auto-zoom on iOS */
    padding: 10px 12px;
  }

  .calc-btn {
    padding: 14px;
    font-size: 16px;
  }

  /* Chart & Canvas responsiveness */
  .canvas-wrapper {
    padding: 10px;
  }

  #mechanismCanvas {
    width: 100%;
    height: auto;
    max-height: 400px;
    object-fit: contain;
  }

  /* Slider touch target */
  input[type="range"] {
    height: 6px;
  }

  input[type="range"]::-webkit-slider-thumb {
    width: 24px;
    height: 24px;
  }
}

@media (max-width: 600px) {
  .quick-stats {
    grid-template-columns: 1fr;
    /* Stack stats */
    gap: 10px;
  }

  .stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
  }

  .stat-label {
    margin-bottom: 0;
  }

  .angle-control .slider-row {
    flex-direction: column;
    align-items: stretch;
  }

  .angle-val {
    text-align: center;
    margin-bottom: 5px;
  }
}