:root {
  --primary: #2c3e50;
  --accent: #8e44ad;
  --correct: #27ae60;
  --wrong: #c0392b;
  --light: #f8f9fa;
  --easy: #3498db;
  --medium: #f39c12;
  --hard: #e74c3c;
  
  /* Energy bar colors */
  --ke-color: #3498db;
  --ke-dark: #1a5276;
  --gpe-color: #27ae60;
  --gpe-dark: #145a32;
  --spe-color: #8e44ad;
  --spe-dark: #512e5f;
}

* { box-sizing: border-box; }

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  margin: 0;
  padding: 20px;
  background: var(--light);
  color: #222;
  line-height: 1.5;
}

.container { max-width: 1000px; margin: 0 auto; }
h1 { text-align: center; color: var(--primary); margin-bottom: 5px; }
.subtitle { text-align: center; color: #666; margin-bottom: 20px; }

/* ==================== LOGIN ==================== */
.login-screen {
  max-width: 400px;
  margin: 80px auto;
  background: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.login-screen h1 { margin-bottom: 10px; }
.login-screen .subtitle { margin-bottom: 30px; }
.login-form { display: flex; flex-direction: column; gap: 15px; }
.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group label { font-weight: 600; font-size: 0.9rem; color: #555; }
.form-group input {
  padding: 12px 15px;
  font-size: 1rem;
  border: 2px solid #ddd;
  border-radius: 6px;
}
.form-group input:focus { outline: none; border-color: var(--accent); }
.login-btn {
  padding: 14px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  margin-top: 10px;
}
.login-btn:hover { background: #7d3c98; }
.login-btn:disabled { background: #ccc; cursor: wait; }
.login-error { color: var(--wrong); font-size: 0.9rem; text-align: center; display: none; }
.login-error.show { display: block; }
.name-hint { font-size: 0.8rem; color: #666; text-align: center; margin: -5px 0 10px 0; }

/* ==================== USER BAR ==================== */
.user-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  padding: 10px 20px;
  border-radius: 8px;
  margin-bottom: 15px;
  box-shadow: 0 1px 5px rgba(0,0,0,0.1);
}
.user-info { font-weight: 600; color: var(--primary); }
.user-info span { color: var(--accent); }
.sync-status { font-size: 0.8rem; color: #888; }
.sync-status.saving { color: var(--medium); }
.sync-status.saved { color: var(--correct); }
.sync-status.error { color: var(--wrong); }
.logout-btn {
  padding: 6px 15px;
  background: #eee;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
}
.logout-btn:hover { background: #ddd; }

/* ==================== MAIN APP ==================== */
.app-screen { display: none; }
.app-screen.active { display: block; }
.login-screen.hidden { display: none; }

/* Tabs */
.tabs { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 15px; justify-content: center; }
.tab-btn {
  padding: 8px 12px;
  border: 2px solid var(--primary);
  background: white;
  color: var(--primary);
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.8rem;
  transition: all 0.2s;
  position: relative;
}
.tab-btn:hover { background: var(--light); }
.tab-btn.active { background: var(--primary); color: white; }
.tab-btn.complete::after {
  content: "✓";
  position: absolute;
  top: -8px;
  right: -8px;
  background: var(--correct);
  color: white;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Progress */
.progress-section {
  background: white;
  padding: 15px 20px;
  border-radius: 8px;
  margin-bottom: 15px;
  box-shadow: 0 1px 5px rgba(0,0,0,0.1);
}
.progress-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.progress-title { font-weight: 700; color: var(--primary); }
.points-display { font-size: 1.2rem; font-weight: 700; }
.points-current { color: var(--accent); }
.progress-bar-container { height: 12px; background: #eee; border-radius: 6px; overflow: hidden; }
.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #9b59b6);
  border-radius: 6px;
  transition: width 0.3s ease;
}
.progress-bar.complete { background: linear-gradient(90deg, var(--correct), #2ecc71); }
.stats-bar { display: flex; justify-content: center; gap: 30px; margin-top: 10px; font-size: 0.85rem; color: #666; }

/* Difficulty */
.difficulty-section { display: flex; gap: 10px; justify-content: center; margin-bottom: 15px; }
.diff-btn {
  padding: 10px 20px;
  border: 2px solid #ddd;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  transition: all 0.2s;
}
.diff-btn:hover { transform: translateY(-2px); }
.diff-btn.easy { border-color: var(--easy); color: var(--easy); }
.diff-btn.easy:hover, .diff-btn.easy.active { background: var(--easy); color: white; }
.diff-btn.medium { border-color: var(--medium); color: var(--medium); }
.diff-btn.medium:hover, .diff-btn.medium.active { background: var(--medium); color: white; }
.diff-btn.hard { border-color: var(--hard); color: var(--hard); }
.diff-btn.hard:hover, .diff-btn.hard.active { background: var(--hard); color: white; }
.diff-points { font-size: 0.75rem; opacity: 0.8; }

/* ==================== PROBLEM CARD - TWO COLUMN LAYOUT ==================== */
.problem-card {
  background: white;
  border-radius: 12px;
  padding: 25px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}

.problem-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 25px;
}

/* Desktop: Two columns */
@media (min-width: 900px) {
  .problem-layout {
    grid-template-columns: 1.3fr 1fr;
    align-items: start;
  }
  
  .answer-panel {
    position: sticky;
    top: 20px;
    background: #fafafa;
    padding: 20px;
    border-radius: 10px;
    border: 1px solid #eee;
  }
}

.problem-content { }

.problem-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px; }
.topic-label {
  display: inline-block;
  background: var(--accent);
  color: white;
  padding: 3px 10px;
  border-radius: 15px;
  font-size: 0.8rem;
  font-weight: 600;
}
.difficulty-badge { padding: 3px 10px; border-radius: 15px; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; }
.difficulty-badge.easy { background: #e8f4fc; color: var(--easy); }
.difficulty-badge.medium { background: #fef5e7; color: var(--medium); }
.difficulty-badge.hard { background: #fdedec; color: var(--hard); }
.problem-text { font-size: 1.05rem; margin-bottom: 15px; line-height: 1.6; }
.given-info { background: #f4f4f4; padding: 12px 15px; border-radius: 6px; margin-bottom: 15px; font-size: 0.95rem; }

/* Graph */
.graph-container {
  background: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 15px;
  display: flex;
  justify-content: center;
}
.graph-container svg { max-width: 100%; }

/* ==================== ANSWER PANEL ==================== */
.answer-panel { }

.answer-section { margin-bottom: 15px; }

.input-row {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.answer-input {
  padding: 10px 15px;
  font-size: 1.1rem;
  border: 2px solid #ddd;
  border-radius: 6px;
  flex: 1;
  min-width: 0;
  -moz-appearance: textfield;
}
.answer-input::-webkit-outer-spin-button,
.answer-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.answer-input:focus { outline: none; border-color: var(--accent); }
.answer-input.wrong-shake {
  animation: shake 0.4s;
  border-color: var(--wrong);
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}

.unit-select {
  padding: 10px 12px;
  font-size: 1rem;
  border: 2px solid #ddd;
  border-radius: 6px;
  background: white;
  cursor: pointer;
  min-width: 70px;
}
.unit-select:focus { outline: none; border-color: var(--accent); }

.button-row {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.check-btn {
  flex: 1;
  padding: 12px 25px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
.check-btn:hover { background: #7d3c98; }

.next-btn {
  flex: 1;
  padding: 12px 25px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

/* Attempt indicator */
.attempt-indicator {
  font-size: 0.85rem;
  color: #666;
  display: block;
  text-align: center;
}
.attempt-indicator .attempt-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ddd;
  margin-left: 3px;
}
.attempt-indicator .attempt-dot.used {
  background: var(--wrong);
}

/* Feedback */
.feedback { margin-top: 15px; padding: 15px; border-radius: 8px; display: none; }
.feedback.correct { background: #d4edda; border: 2px solid var(--correct); color: #155724; display: block; }
.feedback.wrong { background: #f8d7da; border: 2px solid var(--wrong); color: #721c24; display: block; }
.feedback.tryagain { background: #fff3cd; border: 2px solid #ffc107; color: #856404; display: block; }
.feedback.wrong-unit { background: #fff3cd; border: 2px solid #ffc107; color: #856404; display: block; }
.feedback.warning { background: #fff3cd; border: 2px solid #ffc107; color: #856404; display: block; }
.points-earned { font-weight: 700; margin-left: 10px; }
.points-lost { font-weight: 700; margin-left: 10px; color: var(--wrong); }

/* Hint */
.hint-btn { 
  background: none; 
  border: none; 
  color: var(--accent); 
  cursor: pointer; 
  font-size: 0.9rem; 
  text-decoration: underline; 
  margin-top: 10px;
  display: block;
  width: 100%;
  text-align: left;
}
.hint-btn.unlocked { color: var(--correct); }
.hint-text { margin-top: 10px; padding: 10px; background: #faf5fc; border-left: 3px solid var(--accent); font-size: 0.9rem; display: none; }
.hint-text.show { display: block; }

/* Celebration */
.celebration { display: none; text-align: center; padding: 20px; background: linear-gradient(135deg, #d4edda, #c3e6cb); border-radius: 12px; margin-bottom: 15px; }
.celebration.show { display: block; animation: pop 0.3s ease; }
@keyframes pop { 0% { transform: scale(0.8); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
.celebration h2 { color: var(--correct); margin: 0 0 10px 0; }

/* Reset */
.reset-section { text-align: center; margin-top: 20px; }
.reset-btn { padding: 8px 20px; background: #eee; border: none; border-radius: 4px; cursor: pointer; font-size: 0.9rem; margin: 0 5px; }
.reset-btn:hover { background: #ddd; }

/* ==================== SYSTEM CHECK (CONSERVATION) ==================== */
.system-check {
  background: #f0f8ff;
  border: 1px solid #d6e9ff;
  padding: 12px 15px;
  border-radius: 8px;
  margin-bottom: 15px;
}

.system-check-title {
  margin: 0 0 2px 0;
  font-size: 0.9rem;
  font-weight: 800;
  color: var(--primary);
}

.system-check-subtitle {
  margin: 0 0 10px 0;
  font-size: 0.8rem;
  color: #666;
}

.system-check-options {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
}

.system-check-item {
  font-size: 0.9rem;
  color: #222;
  user-select: none;
}

.system-check-item input {
  margin-right: 6px;
  transform: translateY(1px);
}

/* ==================== INTERACTIVE ENERGY BAR CHART ==================== */
.energy-chart-wrapper {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border: 2px solid #ddd;
  border-radius: 12px;
  padding: 15px;
  margin: 15px 0;
}

.energy-chart-title {
  text-align: center;
  font-weight: bold;
  color: #495057;
  margin-bottom: 12px;
  font-size: 0.95rem;
}

.energy-chart {
  display: flex;
  height: 200px;
  position: relative;
  background: white;
  border-radius: 8px;
  padding: 10px;
  border: 1px solid #ddd;
}

.energy-y-axis {
  width: 35px;
  height: 160px;
  position: relative;
  border-right: 2px solid #333;
  margin-right: 10px;
  flex-shrink: 0;
}

.y-tick {
  position: absolute;
  right: 8px;
  font-size: 0.65rem;
  color: #666;
  transform: translateY(50%);
}

.energy-bars-area {
  display: flex;
  align-items: flex-end;
  justify-content: space-around;
  flex: 1;
  height: 160px;
  position: relative;
  padding: 0 10px;
}

.total-line {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 3px dashed #e74c3c;
  z-index: 5;
  pointer-events: none;
}

.total-label {
  position: absolute;
  right: 5px;
  top: -20px;
  font-size: 0.7rem;
  color: #e74c3c;
  font-weight: bold;
  background: white;
  padding: 2px 5px;
  border-radius: 3px;
  white-space: nowrap;
}

.bar-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
}

.energy-bar {
  width: 55px;
  border-radius: 6px 6px 0 0;
  position: relative;
  transition: height 0.05s ease-out, box-shadow 0.2s;
  min-height: 3px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.energy-bar.draggable {
  cursor: ns-resize;
}

.energy-bar.draggable:hover {
  filter: brightness(1.1);
  box-shadow: 0 0 15px rgba(0,0,0,0.25);
}

.energy-bar.dragging {
  filter: brightness(1.15);
  box-shadow: 0 0 20px rgba(0,0,0,0.35);
  z-index: 10;
}

.ke-bar {
  background: linear-gradient(to top, var(--ke-dark), var(--ke-color));
  border: 2px solid var(--ke-dark);
}

.gpe-bar {
  background: linear-gradient(to top, var(--gpe-dark), var(--gpe-color));
  border: 2px solid var(--gpe-dark);
}

.spe-bar {
  background: linear-gradient(to top, var(--spe-dark), var(--spe-color));
  border: 2px solid var(--spe-dark);
}

.bar-val {
  position: absolute;
  top: -24px;
  font-size: 0.8rem;
  font-weight: bold;
  white-space: nowrap;
  text-shadow: 0 1px 2px rgba(255,255,255,0.8);
}

.ke-bar .bar-val { color: var(--ke-dark); }
.gpe-bar .bar-val { color: var(--gpe-dark); }
.spe-bar .bar-val { color: var(--spe-dark); }

.bar-lbl {
  margin-top: 8px;
  font-weight: bold;
  font-size: 0.85rem;
}

.ke-lbl { color: var(--ke-color); }
.gpe-lbl { color: var(--gpe-color); }
.spe-lbl { color: var(--spe-color); }

.chart-hint {
  text-align: center;
  font-size: 0.75rem;
  color: #666;
  font-style: italic;
  margin-top: 10px;
  padding: 6px;
  background: #fff3cd;
  border-radius: 6px;
  border: 1px solid #ffc107;
}

.sum-display {
  text-align: center;
  font-size: 0.9rem;
  font-weight: bold;
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.3s;
}

.sum-display.valid {
  background: #d4edda;
  color: #155724;
  border: 2px solid var(--correct);
}

.sum-display.invalid {
  background: #f8d7da;
  color: #721c24;
  border: 2px solid var(--wrong);
}

/* ==================== RESPONSIVE ==================== */
@media (max-width: 600px) {
  .tabs { gap: 4px; }
  .tab-btn { padding: 6px 8px; font-size: 0.7rem; }
  .problem-card { padding: 15px; }
  .difficulty-section { flex-direction: column; align-items: center; }
  .diff-btn { width: 80%; }
  .login-screen { margin: 40px 15px; padding: 25px; }
  .energy-bar { width: 45px; }
}

/* =========================================================
   HANDOUT THEME OVERRIDES (add body class="handout")
   Keeps new functionality, restores Phab handout aesthetic.
   ========================================================= */
body.handout{
  background:#cfcfcf;
  color:#111;
  font-family:"Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body.handout .experimental-banner{
  background:#f8f9fa;
  color:var(--primary);
  border-bottom:2px solid var(--primary);
  letter-spacing:0;
}

body.handout .experimental-banner span{
  background:transparent;
  border:1px solid var(--border);
  color:var(--primary);
}

body.handout .app-header{
  background:#fff;
  color:var(--primary);
  box-shadow:none;
  border-bottom:3px solid var(--primary);
  padding:14px 18px;
}

body.handout .app-header h1{
  font-size:1.35rem;
  letter-spacing:0;
}

body.handout .app-header .subtitle{
  color:#444;
  opacity:1;
  font-size:0.95rem;
}

body.handout .app-container{
  max-width:820px;
  background:#fff;
  border:1px solid #bbb;
  border-top:none;
  padding:18px 20px 22px;
  margin:0 auto 24px;
}

body.handout .progress-section,
body.handout .problem-card{
  border-radius:0;
  box-shadow:none;
  border:1px solid var(--border);
}

body.handout .progress-section{ padding:10px 12px; }
body.handout .problem-card{ padding:14px 16px; }

body.handout .tab-btn{
  background:#f4f6f8;
  color:var(--primary);
  border:1px solid var(--border);
  border-radius:6px;
  box-shadow:none;
}

body.handout .tab-btn:hover{ transform:none; background:#eef1f4; }
body.handout .tab-btn.active{ background:var(--primary); color:#fff; }
body.handout .tab-btn.complete{ background:#e7f4ec; color:#0b4f2f; border-color:#bfe6cc; }
body.handout .tab-btn.complete.active{ background:#1e8449; color:#fff; border-color:#1e8449; }

body.handout .diff-btn{
  background:#fff;
  color:var(--primary);
  border:1px solid var(--border);
  border-radius:6px;
}

body.handout .diff-btn:hover{ background:#f7f7f7; }
body.handout .diff-btn.active.easy{ border-color:var(--success); }
body.handout .diff-btn.active.medium{ border-color:var(--warning); }
body.handout .diff-btn.active.hard{ border-color:var(--danger); }

body.handout .btn{ border-radius:6px; }
body.handout .btn.primary{ background:var(--accent); }
body.handout .btn.success{ background:var(--success); }
body.handout .btn.primary:hover,
body.handout .btn.success:hover{ transform:none; box-shadow:none; filter:brightness(0.96); }

body.handout .energy-chart-wrapper{
  background:#fff;
  border:1px solid var(--border);
  border-radius:0;
  box-shadow:none;
  max-width:420px;
}

body.handout .energy-chart{ border-radius:0; background:#fff; }

body.handout .ke-bar{ background:var(--ke-color); border:1px solid var(--ke-dark); }
body.handout .gpe-bar{ background:var(--gpe-color); border:1px solid var(--gpe-dark); }
body.handout .spe-bar{ background:var(--spe-color); border:1px solid var(--spe-dark); }
body.handout .energy-bar{ border-radius:0; }

body.handout .app-footer{ color:#555; }

/* ==================== CONFETTI ==================== */
#confetti-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: hidden;
  z-index: 9999;
}

.confetti-particle {
  position: absolute;
  top: -10px;
  width: 10px;
  height: 10px;
  opacity: 0;
  animation: confetti-fall 2s ease-out forwards;
}

@keyframes confetti-fall {
  0% {
    opacity: 1;
    transform: translateY(0) rotate(0deg) scale(1);
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translateY(100vh) rotate(720deg) scale(0.3);
  }
}

/* Variation for more natural movement */
.confetti-particle:nth-child(odd) {
  animation-name: confetti-fall-left;
}

.confetti-particle:nth-child(even) {
  animation-name: confetti-fall-right;
}

@keyframes confetti-fall-left {
  0% {
    opacity: 1;
    transform: translateY(0) translateX(0) rotate(0deg) scale(1);
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translateY(100vh) translateX(-100px) rotate(-720deg) scale(0.3);
  }
}

@keyframes confetti-fall-right {
  0% {
    opacity: 1;
    transform: translateY(0) translateX(0) rotate(0deg) scale(1);
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translateY(100vh) translateX(100px) rotate(720deg) scale(0.3);
  }
}
