/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --clr-bg: #1a1a2e;
  --clr-surface: #ffffff;
  --clr-primary: #4361ee;
  --clr-primary-hover: #3a56d4;
  --clr-text: #1e293b;
  --clr-text-muted: #64748b;
  --clr-text-light: #e2e8f0;
  --clr-border: #cbd5e1;
  --clr-success: #16a34a;
  --clr-error: #dc2626;
  --clr-warning: #f59e0b;
  --clr-tray-bg: #f1f5f9;
  --radius: 10px;
  --shadow: 0 2px 6px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.18);
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

html { font-size: 16px; }
body {
  font-family: var(--font);
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  color: var(--clr-text);
  line-height: 1.5;
  min-height: 100vh;
}
body.embed { background: transparent; }

/* === Layout === */
#app {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1rem;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  background: rgba(255,255,255,0.1);
  backdrop-filter: blur(8px);
  border-radius: var(--radius);
  margin-bottom: 1rem;
}
header h1 {
  font-size: 1.3rem;
  font-weight: 700;
  color: #fff;
}
body.embed header { display: none; }

.lang-toggle {
  display: flex;
  gap: 0.25rem;
}
.lang-toggle button {
  padding: 0.3rem 0.6rem;
  border: 1px solid rgba(255,255,255,0.3);
  background: rgba(255,255,255,0.1);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 700;
  color: rgba(255,255,255,0.7);
  transition: all 0.15s;
}
.lang-toggle button.active {
  background: var(--clr-primary);
  color: #fff;
  border-color: var(--clr-primary);
}

/* === Phase containers === */
.phase { display: none; }
.phase.active { display: block; }

/* === Card === */
.card {
  background: var(--clr-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  margin-bottom: 1rem;
}
.card h2 {
  font-size: 1.1rem;
  margin-bottom: 0.75rem;
  color: var(--clr-primary);
}

/* === Persistent Prescription Panel === */
#prescription-panel {
  border-left: 4px solid var(--clr-primary);
  background: rgba(255,255,255,0.95);
  margin-bottom: 1rem;
}
.panel-patient-row {
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
}
.panel-patient-meta {
  color: var(--clr-text-muted);
}
.panel-allergies {
  color: var(--clr-text-muted);
}

/* === Intro Phase === */
.patient-info {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.25rem 1rem;
  margin-bottom: 1rem;
}
.patient-info dt {
  font-weight: 600;
  color: var(--clr-text-muted);
  font-size: 0.9rem;
}
.patient-info dd {
  font-size: 0.9rem;
}
.allergy-tag {
  display: inline-block;
  background: #fef2f2;
  color: var(--clr-error);
  border: 1px solid #fecaca;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
  font-weight: 700;
  margin-right: 0.25rem;
}

.rx-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
.rx-table th {
  text-align: left;
  padding: 0.5rem;
  border-bottom: 2px solid var(--clr-border);
  color: var(--clr-text-muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.rx-table td {
  padding: 0.5rem;
  border-bottom: 1px solid var(--clr-border);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.65rem 1.6rem;
  background: var(--clr-primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  box-shadow: 0 2px 8px rgba(67,97,238,0.3);
}
.btn:hover { background: var(--clr-primary-hover); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}
.btn--success {
  background: var(--clr-success);
  box-shadow: 0 2px 8px rgba(22,163,74,0.3);
}
.btn--success:hover { background: #15803d; }
.btn--outline {
  background: transparent;
  color: var(--clr-primary);
  border: 2px solid var(--clr-primary);
  box-shadow: none;
}
.btn--outline:hover {
  background: var(--clr-primary);
  color: #fff;
}

/* === Prescription name display === */
.rx-brand {
  display: block;
  font-weight: 700;
  font-size: 0.9rem;
}
.rx-generic {
  display: block;
  font-size: 0.75rem;
  color: var(--clr-text-muted);
  font-style: italic;
}

/* === eResepti detail === */
.rx-dosing {
  font-weight: 700;
  font-family: monospace;
  font-size: 0.95rem;
  letter-spacing: 0.05em;
}
.rx-detail-toggle {
  display: inline-block;
  margin-top: 0.5rem;
  padding: 0.3rem 0.7rem;
  background: none;
  border: 1px solid var(--clr-border);
  border-radius: 6px;
  font-size: 0.8rem;
  color: var(--clr-primary);
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s;
}
.rx-detail-toggle::before {
  content: '\25B6 ';
  font-size: 0.65rem;
}
.rx-detail-toggle.open::before {
  content: '\25BC ';
}
.rx-detail-toggle:hover {
  background: #eef2ff;
}
.rx-detail-panel {
  margin-top: 0.5rem;
  padding: 0.75rem;
  background: #f8fafc;
  border: 1px solid var(--clr-border);
  border-radius: 6px;
}
.rx-detail-table {
  font-size: 0.8rem;
}

/* === Fimea link === */
.fimea-link {
  display: inline-block;
  margin-top: 0.5rem;
  margin-left: 0.75rem;
  font-size: 0.8rem;
  color: var(--clr-primary);
  text-decoration: none;
  font-weight: 600;
}
.fimea-link:hover {
  text-decoration: underline;
}
.fimea-link::before {
  content: '\1F517 ';
}

/* === Checklist === */
.checklist-list {
  list-style: none;
}
.checklist-list li {
  padding: 0.7rem 0;
  border-bottom: 1px solid var(--clr-border);
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.checklist-list li:last-child { border-bottom: none; }
.checklist-list input[type="checkbox"] {
  width: 1.4rem;
  height: 1.4rem;
  accent-color: var(--clr-primary);
  flex-shrink: 0;
}
.checklist-list label {
  cursor: pointer;
  user-select: none;
  font-size: 0.95rem;
}
.checklist-warning {
  color: var(--clr-error);
  font-size: 0.85rem;
  margin-top: 0.5rem;
  display: none;
}
.checklist-warning.visible { display: block; }

/* === Dealing Phase === */
.dealing-layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 1rem;
  align-items: start;
}
@media (max-width: 700px) {
  .dealing-layout {
    grid-template-columns: 1fr;
  }
}

.tray-column {
  position: sticky;
  top: 0.5rem;
  max-height: calc(100vh - 1rem);
  overflow-y: auto;
}

.tray {
  background: var(--clr-surface);
  border: 1px solid var(--clr-border);
  border-radius: var(--radius);
  padding: 0.75rem;
  box-shadow: var(--shadow);
}

/* === Pill Splitter === */
.splitter {
  background: var(--clr-surface);
  border: 2px dashed #94a3b8;
  border-radius: var(--radius);
  padding: 0.75rem;
  text-align: center;
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
  box-shadow: var(--shadow);
  min-height: 80px;
  margin-top: 1rem;
}
.splitter.drag-over {
  border-color: var(--clr-primary);
  background: #eef2ff;
  box-shadow: 0 0 12px rgba(67, 97, 238, 0.25);
}
.splitter.error-flash {
  border-color: var(--clr-error);
  background: #fef2f2;
}
.splitter-icon {
  margin-bottom: 0.25rem;
}
.splitter-label {
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--clr-text);
}
.splitter-hint {
  font-size: 0.75rem;
  color: var(--clr-text-muted);
  margin-top: 0.15rem;
}
.splitter-error-msg {
  font-size: 0.8rem;
  color: var(--clr-error);
  font-weight: 600;
  margin-top: 0.3rem;
  animation: shake 0.4s ease;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-4px); }
  40%, 80% { transform: translateX(4px); }
}
.splitter-halves {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.5rem;
  justify-content: center;
}
.splitter-half-item {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.3rem 0.5rem;
  background: #f8fafc;
  border: 2px solid var(--clr-border);
  border-radius: 6px;
  cursor: grab;
  font-size: 0.75rem;
  font-weight: 600;
  user-select: none;
  touch-action: none;
  transition: border-color 0.15s, transform 0.1s;
}
.splitter-half-item:hover {
  border-color: var(--clr-primary);
  transform: scale(1.05);
}
.splitter-half-item:active {
  cursor: grabbing;
  transform: scale(0.95);
}
.splitter-half-item .pill-preview {
  width: 22px;
  height: 22px;
  display: inline-block;
}
.tray h3 {
  font-size: 0.85rem;
  color: var(--clr-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.75rem;
  padding-bottom: 0.4rem;
  border-bottom: 2px solid var(--clr-primary);
}

.tray-item {
  background: var(--clr-tray-bg);
  border: 1px solid var(--clr-border);
  border-radius: 8px;
  padding: 0.6rem;
  margin-bottom: 0.5rem;
  transition: box-shadow 0.15s, border-color 0.15s;
}
.tray-item:hover {
  box-shadow: var(--shadow);
  border-color: var(--clr-primary);
}
.tray-item-name {
  font-weight: 700;
  font-size: 0.9rem;
  margin-bottom: 0.1rem;
}
.tray-item-details {
  font-size: 0.78rem;
  color: var(--clr-text-muted);
  margin-bottom: 0.4rem;
}
.tray-item-handles {
  display: flex;
  gap: 0.4rem;
}
.drag-handle {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.65rem;
  border-radius: 6px;
  border: 2px solid var(--clr-border);
  background: var(--clr-surface);
  cursor: grab;
  font-size: 0.8rem;
  font-weight: 700;
  user-select: none;
  touch-action: none;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}
.drag-handle:hover {
  border-color: var(--clr-primary);
  background: #eef2ff;
  transform: scale(1.03);
}
.drag-handle:active { cursor: grabbing; transform: scale(0.97); }
.splittable-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fef3c7;
  border: 1.5px solid #f59e0b;
  color: #92400e;
  font-size: 0.75rem;
  font-weight: 800;
  flex-shrink: 0;
  cursor: help;
}
.drag-handle .pill-preview {
  width: 22px;
  height: 22px;
  display: inline-block;
}

.dosette-area {
  position: relative;
}
.dosette-area h3 {
  font-size: 0.85rem;
  color: rgba(255,255,255,0.8);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.5rem;
}
#dosette-canvas {
  border: 2px solid rgba(255,255,255,0.2);
  border-radius: var(--radius);
  display: block;
  width: 100%;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
}

.dealing-instructions {
  font-size: 0.85rem;
  color: rgba(255,255,255,0.7);
  margin-bottom: 0.75rem;
  font-style: italic;
}
.dealing-actions {
  margin-top: 1rem;
  text-align: right;
}

/* === Drag ghost === */
.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 1000;
  opacity: 0.9;
  transform: translate(-50%, -50%) scale(1.3);
  filter: drop-shadow(0 2px 6px rgba(0,0,0,0.3));
}

/* === Popover === */
.popover-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.4);
  z-index: 500;
}
.popover-overlay.visible { display: block; }
.popover {
  display: none;
  position: fixed;
  z-index: 501;
  background: var(--clr-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 1rem;
  min-width: 240px;
  max-width: 340px;
}
.popover.visible { display: block; }
.popover h4 {
  font-size: 0.95rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
  color: var(--clr-primary);
}
.popover-pill-list {
  list-style: none;
}
.popover-pill-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0;
  border-bottom: 1px solid var(--clr-border);
  font-size: 0.85rem;
}
.popover-pill-list li:last-child { border-bottom: none; }
.popover-remove-btn {
  background: var(--clr-error);
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 0.2rem 0.6rem;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.popover-remove-btn:hover { background: #b91c1c; }
.popover-close {
  margin-top: 0.75rem;
  width: 100%;
  padding: 0.5rem;
  background: #e2e8f0;
  color: var(--clr-text);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  transition: background 0.15s;
}
.popover-close:hover { background: #cbd5e1; }
.popover-empty {
  color: var(--clr-text-muted);
  font-size: 0.85rem;
  font-style: italic;
}

/* === Feedback Phase === */
.feedback-summary {
  text-align: center;
  padding: 1.5rem;
}
.feedback-summary.pass h2 { color: var(--clr-success); font-size: 1.3rem; }
.feedback-summary.fail h2 { color: var(--clr-error); font-size: 1.3rem; }
.feedback-score {
  font-size: 2rem;
  font-weight: 800;
  margin: 0.5rem 0;
}
.feedback-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin-top: 1rem;
}

.feedback-detail {
  margin-top: 1rem;
}
.feedback-cell {
  padding: 0.5rem;
  border-bottom: 1px solid var(--clr-border);
  font-size: 0.85rem;
}
.feedback-cell:last-child { border-bottom: none; }
.feedback-cell-key {
  font-weight: 700;
  margin-right: 0.5rem;
}
.feedback-cell.correct .feedback-cell-key::before {
  content: '\2713 ';
  color: var(--clr-success);
}
.feedback-cell.incorrect .feedback-cell-key::before {
  content: '\2717 ';
  color: var(--clr-error);
}
.feedback-cell.incorrect {
  background: #fef2f2;
  border-radius: 6px;
}
.feedback-expected, .feedback-placed {
  display: inline;
  font-size: 0.8rem;
}
.feedback-expected { color: var(--clr-text-muted); }

/* === Scenario Selector === */
.select-card {
  max-width: 800px;
  margin: 2rem auto;
}
.select-card h2 {
  text-align: center;
  font-size: 1.4rem;
  margin-bottom: 1.25rem;
}
.scenario-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
}
.scenario-item {
  background: var(--clr-tray-bg);
  border: 2px solid var(--clr-border);
  border-radius: var(--radius);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.15s;
}
.scenario-item:hover {
  border-color: var(--clr-primary);
  box-shadow: var(--shadow-lg);
  transform: translateY(-2px);
}
.scenario-item h3 {
  font-size: 1.05rem;
  color: var(--clr-text);
}
.scenario-item p {
  font-size: 0.85rem;
  color: var(--clr-text-muted);
  flex: 1;
}
.scenario-item .btn {
  align-self: flex-start;
  margin-top: auto;
  padding: 0.45rem 1.2rem;
  font-size: 0.85rem;
}
.scenario-difficulty {
  display: inline-block;
  align-self: flex-start;
  padding: 0.2rem 0.6rem;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.scenario-difficulty.diff-1 {
  background: #dcfce7;
  color: #166534;
  border: 1px solid #86efac;
}
.scenario-difficulty.diff-2 {
  background: #fef9c3;
  color: #854d0e;
  border: 1px solid #fde047;
}
.scenario-difficulty.diff-3 {
  background: #fee2e2;
  color: #991b1b;
  border: 1px solid #fca5a5;
}

/* === Confetti Canvas === */
#confetti-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 2000;
  display: none;
}
#confetti-canvas.active {
  display: block;
}

/* === Celebration (pass) === */
.celebration-trophy {
  font-size: 4rem;
  margin-bottom: 0.25rem;
  animation: trophy-bounce 0.8s ease;
}
@keyframes trophy-bounce {
  0% { transform: scale(0.3) rotate(-10deg); opacity: 0; }
  50% { transform: scale(1.2) rotate(5deg); opacity: 1; }
  70% { transform: scale(0.95) rotate(-2deg); }
  100% { transform: scale(1) rotate(0deg); }
}
.celebration-stars {
  font-size: 1.8rem;
  margin: 0.3rem 0;
  animation: stars-appear 1s ease 0.3s both;
}
@keyframes stars-appear {
  0% { opacity: 0; transform: scale(0.5); }
  60% { transform: scale(1.15); }
  100% { opacity: 1; transform: scale(1); }
}
.feedback-summary.pass {
  background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 50%, #bbf7d0 100%);
  border: 2px solid #86efac;
}
.feedback-summary.pass h2 {
  font-size: 1.4rem;
  color: #166534;
}

/* === Light-hearted fail === */
.fail-emoji {
  font-size: 3rem;
  margin-bottom: 0.25rem;
  animation: fail-wiggle 0.6s ease;
}
@keyframes fail-wiggle {
  0% { transform: rotate(0deg); }
  25% { transform: rotate(-8deg); }
  50% { transform: rotate(8deg); }
  75% { transform: rotate(-4deg); }
  100% { transform: rotate(0deg); }
}
.feedback-summary.fail {
  background: linear-gradient(135deg, #fffbeb 0%, #fef3c7 100%);
  border: 2px solid #fde68a;
}
.feedback-summary.fail h2 {
  font-size: 1.15rem;
  color: #92400e;
}

/* === Error === */
.error-message {
  text-align: center;
  padding: 3rem 1rem;
  color: #fca5a5;
  background: rgba(255,255,255,0.05);
  border-radius: var(--radius);
}

/* === Responsive === */
@media (max-width: 700px) {
  .dealing-layout {
    overflow-x: auto;
  }
  #dosette-canvas {
    min-width: 650px;
  }
  .rx-table { font-size: 0.8rem; }
  .rx-table th, .rx-table td { padding: 0.35rem; }
}
