@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&family=JetBrains+Mono:wght@400;500;700&display=swap');

:root {
  --primary: #f59e0b;
  --primary-hover: #d97706;
  --bg: #fffbeb;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --success: #22c55e;
  --error: #ef4444;
  --blue: #2563eb;
}

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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: background-color 0.3s ease;
}

/* Active Game Optimizations to turn the app into a full-screen, scroll-free experience matching native apps */
body.game-active {
  background-color: #ffffff !important;
  padding: 0 !important;
  margin: 0 !important;
  height: 100vh !important;
  height: 100dvh !important;
  min-height: 100vh !important;
  min-height: 100dvh !important;
  overflow: hidden !important;
  display: flex;
  flex-direction: column;
}

body.game-active header, body.game-active footer {
  display: none !important;
}

body.game-active #root {
  max-width: 100% !important;
  width: 100% !important;
  height: 100vh !important;
  height: 100dvh !important;
  padding: 0 !important;
  margin: 0 !important;
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  overflow: hidden !important;
}

body.game-active .game-card {
  box-shadow: none !important;
  border: none !important;
  border-radius: 0 !important;
  width: 100% !important;
  max-width: 100% !important;
  height: 100vh !important;
  height: 100dvh !important;
  margin: 0 !important;
  padding: 6px 12px !important;
  display: flex !important;
  flex-direction: column !important;
  justify-content: flex-start !important;
  align-items: center !important;
  flex-grow: 1 !important;
  background: white !important;
  overflow: hidden !important;
}

/* Sizing configurations using robust view, orientation, and height queries */
:root {
  --cell-w: 44px;
  --cell-h: 46px;
  --cell-font: 1.4rem;
  --divisor-font: 2.5rem;
  --numpad-btn-h: 52px;
  --numpad-gap: 8px;
  --action-btn-h: 48px;
}

/* Portable/Medium screen adjustments (e.g., standard tablets, large mobiles) */
@media (max-width: 640px) {
  :root {
    --cell-w: 36px;
    --cell-h: 38px;
    --cell-font: 1.25rem;
    --divisor-font: 2rem;
    --numpad-btn-h: 44px;
    --numpad-gap: 6px;
    --action-btn-h: 42px;
  }
}

/* Portrait heights optimizations so that it perfectly fits to screen */
@media (max-height: 850px) and (orientation: portrait) {
  :root {
    --cell-w: 32px;
    --cell-h: 34px;
    --cell-font: 1.15rem;
    --divisor-font: 1.8rem;
    --numpad-btn-h: 40px;
    --numpad-gap: 5px;
    --action-btn-h: 38px;
  }
  body.game-active .game-card {
    padding: 6px 12px !important;
  }
}

@media (max-height: 720px) and (orientation: portrait) {
  :root {
    --cell-w: 28px;
    --cell-h: 30px;
    --cell-font: 1.05rem;
    --divisor-font: 1.6rem;
    --numpad-btn-h: 34px;
    --numpad-gap: 4px;
    --action-btn-h: 34px;
  }
  body.game-active .game-card {
    padding: 4px 8px !important;
  }
}

@media (max-height: 600px) and (orientation: portrait) {
  :root {
    --cell-w: 24px;
    --cell-h: 26px;
    --cell-font: 0.95rem;
    --divisor-font: 1.4rem;
    --numpad-btn-h: 28px;
    --numpad-gap: 3px;
    --action-btn-h: 30px;
  }
}

/* Landscape Mode layout optimizations to prevent scrolling and make it fit to screen perfectly */
@media (orientation: landscape) {
  body.game-active .game-card {
    padding: 6px 12px !important;
  }

  body.game-active .game-header-bar {
    margin-bottom: 4px !important;
    padding-bottom: 4px !important;
    gap: 4px !important;
  }

  body.game-active #game-main-row {
    flex-direction: row !important;
    align-items: center !important;
    justify-content: space-around !important;
    gap: 12px !important;
    flex-grow: 1 !important;
    width: 100% !important;
    overflow: hidden !important;
  }

  body.game-active #math-grid-panel {
    flex-grow: 1.2 !important;
    padding: 0 !important;
    display: flex !important;
    justify-content: center !important;
    align-items: flex-start !important; /* Starts from top so dividend and divisor are never lost! */
    overflow-y: auto !important;
  }

  body.game-active #numpad-control-panel {
    flex-grow: 1 !important;
    max-width: 240px !important;
    width: 100% !important;
  }

  body.game-active #game-status-panel {
    margin-top: 4px !important;
  }

  body.game-active .feedback-card {
    padding: 6px 12px !important;
    margin-top: 2px !important;
  }
}

/* Compact Landscape Heights adjustments */
@media (orientation: landscape) and (max-height: 520px) {
  :root {
    --cell-w: 26px;
    --cell-h: 28px;
    --cell-font: 0.95rem;
    --divisor-font: 1.4rem;
    --numpad-btn-h: 30px;
    --numpad-gap: 3px;
    --action-btn-h: 30px;
  }
}

/* Sibling layout styles for centralizing parts */
body.game-active #game-main-row {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  align-items: center !important;
  gap: 8px !important;
  width: 100% !important;
  flex-grow: 1 !important;
  min-height: 0 !important;
  overflow: hidden !important;
}

body.game-active #math-grid-panel {
  display: flex !important;
  justify-content: center !important;
  align-items: flex-start !important; /* Starts from top so dividend and divisor are never lost! */
  width: 100% !important;
  flex-grow: 1 !important;
  min-height: 0 !important;
  overflow-y: auto !important;
  padding: 4px 6px !important;
}

body.game-active #numpad-control-panel {
  flex-shrink: 0 !important;
  width: 100% !important;
  max-width: 270px !important;
  display: flex !important;
  justify-content: center !important;
}

/* Standard Potence Gallows styles mapped with variables */
.potence-row {
  display: flex;
  height: var(--cell-h);
  align-items: center;
}

.potence-cell {
  width: var(--cell-w);
  height: var(--cell-h);
  font-size: var(--cell-font);
  display: flex;
  justify-content: center;
  align-items: center;
}

.potence-separator-row {
  display: flex;
  height: 6px;
  align-items: center;
}

.potence-separator-row .potence-cell {
  height: 6px;
}

.separator-line {
  width: 100%;
  height: 2.5px;
}

.potence-container .math-input {
  width: var(--cell-w);
  height: var(--cell-h);
  font-size: var(--cell-font);
  text-align: center;
  border-radius: 8px;
  border: 1.5px solid var(--border);
  background: #f8fafc;
  outline: none;
  font-family: inherit;
  font-weight: 800;
  transition: all 0.1s ease;
  padding: 0;
}

.potence-container .math-input:focus {
  border-color: var(--primary);
  background: #fffbeb;
}

.divisor-sign {
  font-size: var(--cell-font);
}

.divisor-val {
  font-size: var(--divisor-font);
}

.numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--numpad-gap) !important;
  width: 100%;
}

.num-btn {
  height: var(--numpad-btn-h) !important;
  font-size: calc(var(--cell-font) * 1.05) !important;
  border-radius: 12px;
  padding: 0 !important;
}

.btn-action {
  height: var(--action-btn-h) !important;
  border-radius: 12px;
  padding: 0 8px !important;
  font-size: calc(var(--cell-font) * 0.82) !important;
}

/* Bring-down Animation Indicators and styles with variable bindings */
.bd-indicator-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.bd-indicator-line {
  position: absolute;
  top: calc(-1 * var(--cell-h) - 4px);
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: calc(var(--cell-h) - 2px);
  border-left: 2px dashed #f59e0b;
  opacity: 0.75;
  pointer-events: none;
  animation: strokeDraw 0.6s ease-out forwards;
}

.bd-indicator-arrow {
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 6px solid #f59e0b;
  opacity: 0.9;
  pointer-events: none;
}

@keyframes strokeDraw {
  0% {
    height: 0;
    opacity: 0;
  }
  100% {
    height: calc(var(--cell-h) - 2px);
    opacity: 0.75;
  }
}

@keyframes slideDownFromTop {
  0% {
    transform: translateY(calc(-1 * var(--cell-h)));
    opacity: 0;
  }
  60% {
    transform: translateY(4px);
    opacity: 0.9;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

.animate-slide-down {
  animation: slideDownFromTop 0.65s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@media (min-width: 640px) {
  body {
    padding: 24px;
  }
}

header {
  max-width: 600px;
  width: 100%;
  text-align: center;
  margin-bottom: 24px;
}

.title-container {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  background: white;
  padding: 12px 24px;
  border-radius: 50px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  border: 1px solid #fef3c7;
}

.title-container h1 {
  font-size: 1.5rem;
  font-weight: 800;
  color: #0f172a;
}

main {
  max-width: 1000px;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.card {
  background: white;
  padding: 32px;
  border-radius: 24px;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 500px;
  border: 1px solid var(--border);
}

.hidden {
  display: none !important;
}

/* Config View */
.config-section h2 {
  font-size: 1.25rem;
  margin-bottom: 24px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #475569;
}

.option-group {
  margin-bottom: 24px;
}

.option-label {
  font-size: 0.75rem;
  font-weight: 700;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 12px;
  display: block;
}

.btn-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.btn-row {
  display: flex;
  gap: 12px;
}

.btn-option {
  padding: 16px;
  border-radius: 16px;
  border: 2px solid var(--border);
  background: white;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  flex: 1;
  text-align: left;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.btn-option.active {
  border-color: var(--primary);
  background: #fffbeb;
  color: #b45309;
}

.btn-primary {
  width: 100%;
  background: #0f172a;
  color: white;
  padding: 20px;
  border-radius: 16px;
  font-size: 1.25rem;
  font-weight: 700;
  border: none;
  cursor: pointer;
  margin-top: 24px;
  box-shadow: 0 10px 15px -3px rgba(15, 23, 42, 0.2);
  transition: transform 0.1s;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

.btn-primary:active {
  transform: scale(0.98);
}

/* Game View */
.game-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.game-card {
  background: white;
  padding: 20px;
  border-radius: 24px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 900px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

@media (min-width: 768px) {
  .game-card {
    padding: 32px;
  }
}

.game-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-bottom: 20px;
  border-bottom: 1.5px solid #f1f5f9;
  padding-bottom: 12px;
}

.btn-back {
  background: none;
  border: none;
  color: #94a3b8;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.95rem;
  transition: color 0.2s;
}

.btn-back:hover {
  color: var(--text);
}

.score-badge {
  background: #fef9c3;
  color: #a16207;
  padding: 6px 14px;
  border-radius: 50px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
}

.game-mini-title {
  font-size: 1.2rem;
  font-weight: 800;
  color: #0f172a;
}

.game-split {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 24px;
  align-items: center;
}

@media (min-width: 720px) {
  .game-split {
    flex-direction: row;
    align-items: flex-start;
    justify-content: space-around;
    gap: 32px;
  }
}

.game-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  flex: 1.3;
  overflow-x: auto;
  padding: 8px;
}

.game-right {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 320px;
  flex: 1;
}

/* Calculation Table */
.calc-container {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  width: 100%;
  display: flex;
  justify-content: center;
  overflow-x: auto;
}

.calc-grid {
  display: grid;
  gap: 4px;
  justify-content: end;
}

@media (min-width: 680px) {
  .calc-grid {
    gap: 8px;
  }
}

.calc-row {
  display: flex;
  gap: 4px;
  justify-content: flex-end;
  align-items: center;
  position: relative;
}

@media (min-width: 680px) {
  .calc-row {
    gap: 8px;
  }
}

.cell {
  width: 38px;
  height: 48px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.6rem;
  position: relative;
}

@media (min-width: 680px) {
  .cell {
    width: 58px;
    height: 72px;
    font-size: 2.8rem;
  }
}

.cell input {
  width: 100%;
  height: 100%;
  text-align: center;
  border-radius: 8px;
  border: 2px solid var(--border);
  font-size: inherit;
  font-family: inherit;
  font-weight: inherit;
  background: #f8fafc;
  outline: none;
  transition: all 0.2s;
  caret-color: var(--blue);
}

@media (min-width: 680px) {
  .cell input {
    border-radius: 12px;
    border: 3.5px solid var(--border);
  }
}

.cell input:focus {
  border-color: var(--blue);
  background: #eff6ff;
}

.cell input.focused {
  border-color: var(--blue);
  background: #eff6ff;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.cell input.correct {
  border-color: var(--success) !important;
  background: #f0fdf4 !important;
  color: #15803d !important;
}

.cell input.error {
  border-color: var(--error) !important;
  background: #fef2f2 !important;
  color: #b91c1c !important;
}

/* Numpad */
.numpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  width: 100%;
  max-width: 250px;
  margin-top: 12px;
}

@media (min-width: 680px) {
  .numpad {
    gap: 10px;
    max-width: 280px;
    margin-top: 0;
  }
}

.num-btn {
  background: white;
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  transition: all 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  touch-action: manipulation;
  height: 44px;
}

@media (min-width: 680px) {
  .num-btn {
    border-radius: 14px;
    padding: 12px;
    font-size: 1.35rem;
    height: 52px;
  }
}

.num-btn:hover {
  border-color: var(--blue);
  background: #f8fafc;
}

.num-btn:active {
  background: #eff6ff;
  transform: scale(0.95);
  border-color: var(--blue);
}

.delete-btn {
  color: var(--error);
}

.delete-btn:hover {
  background: #fef2f2;
  border-color: var(--error);
}

.enter-btn {
  background: var(--success);
  color: white;
  border-color: var(--success);
}

.enter-btn:hover {
  background: #16a34a;
  border-color: #16a34a;
}

.enter-btn:active {
  background: #15803d;
}

.operator-symbol {
  position: absolute;
  left: -28px;
  font-size: 1.3rem;
  color: #cbd5e1;
}

@media (min-width: 680px) {
  .operator-symbol {
    left: -48px;
    font-size: 2.2rem;
  }
}

.calc-line {
  height: 4px;
  background: #1e293b;
  border-radius: 4px;
  width: 100%;
  margin: 6px 0;
}

@media (min-width: 680px) {
  .calc-line {
    height: 6px;
    margin: 10px 0;
  }
}

.result-text {
  color: var(--blue);
}

/* Carries & Borrows */
.carries-row {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
  margin-bottom: 4px;
  height: 28px;
}

@media (min-width: 680px) {
  .carries-row {
    gap: 8px;
    margin-bottom: 6px;
    height: 42px;
  }
}

.carry-cell {
  width: 38px;
  display: flex;
  justify-content: center;
  align-items: center;
}

@media (min-width: 680px) {
  .carry-cell {
    width: 58px;
  }
}

.carry-input {
  width: 22px;
  height: 22px;
  text-align: center;
  border-radius: 6px;
  border: 1.5px solid #fde68a;
  background: #fffbeb;
  font-size: 0.8rem;
  outline: none;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  transition: all 0.2s;
}

.carry-input:focus {
  border-color: var(--primary);
  background: #fef3c7;
}

.carry-input.correct {
  border-color: var(--success);
  background: #f0fdf4;
  color: #15803d;
}

.carry-input.error {
  border-color: var(--error);
  background: #fef2f2;
  color: #b91c1c;
}

@media (min-width: 680px) {
  .carry-input {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: 2px solid #fde68a;
    font-size: 1rem;
  }
}

/* Actions */
.game-actions {
  display: flex;
  gap: 8px;
  width: 100%;
  max-width: 250px;
  justify-content: center;
  margin-top: 12px;
}

@media (min-width: 680px) {
  .game-actions {
    gap: 12px;
    max-width: 280px;
    margin-top: 16px;
  }
}

.btn-action {
  flex: 1;
  padding: 8px;
  border-radius: 10px;
  font-size: 0.95rem;
  font-weight: 700;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: transform 0.1s;
  height: 42px;
}

@media (min-width: 680px) {
  .btn-action {
    padding: 12px;
    border-radius: 14px;
    font-size: 1.05rem;
    height: 48px;
  }
}

.btn-verify {
  background: var(--blue);
  color: white;
  box-shadow: 0 10px 15px -3px rgba(37, 99, 235, 0.25);
  flex: 1.3;
}

.btn-new {
  background: #0f172a;
  color: white;
  box-shadow: 0 10px 15px -3px rgba(15, 23, 42, 0.15);
  flex: 1;
}

.btn-action:active {
  transform: scale(0.95);
}

/* Feedback */
.feedback-card {
  width: 100%;
  max-width: 100%;
  padding: 12px 16px;
  border-radius: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  border: 1.5px solid transparent;
  margin-top: 16px;
}

@media (min-width: 680px) {
  .feedback-card {
    padding: 16px 24px;
    border-radius: 20px;
    gap: 16px;
    margin-top: 24px;
  }
}

.feedback-success {
  background: #f0fdf4;
  border-color: #dcfce7;
  color: #166534;
}

.feedback-error {
  background: #fef2f2;
  border-color: #fee2e2;
  color: #991b1b;
}

.feedback-icon {
  width: 32px;
  height: 32px;
}

@media (min-width: 680px) {
  .feedback-icon {
    width: 40px;
    height: 40px;
  }
}

.feedback-content h3 {
  font-size: 1rem;
  font-weight: 700;
}

@media (min-width: 680px) {
  .feedback-content h3 {
    font-size: 1.15rem;
  }
}

.feedback-content button {
  background: none;
  border: none;
  color: inherit;
  text-decoration: underline;
  font-weight: 700;
  cursor: pointer;
  margin-top: 4px;
  font-size: 0.95rem;
}

@media (min-width: 680px) {
  .feedback-content button {
    margin-top: 8px;
    font-size: 1.05rem;
  }
}

footer {
  margin-top: 40px;
  color: #94a3b8;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 16px;
}

@media (min-width: 680px) {
  footer {
    margin-top: 64px;
  }
}

.dot {
  width: 4px;
  height: 4px;
  background: #cbd5e1;
  border-radius: 50%;
}

/* Division-Specific Gallows / Potence layout */
.potence-container {
  display: flex;
  align-items: stretch;
  justify-content: center;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  gap: 0;
  margin: 16px 0;
}

.potence-dividend-block {
  display: grid;
  gap: 4px;
  padding-right: 16px;
  align-content: start;
}

@media (min-width: 680px) {
  .potence-dividend-block {
    gap: 8px;
    padding-right: 24px;
  }
}

.potence-line-middle {
  width: 4px;
  background-color: #1e293b;
  border-radius: 2px;
}

.potence-divisor-block {
  padding-left: 16px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-content: start;
}

@media (min-width: 680px) {
  .potence-divisor-block {
    padding-left: 24px;
  }
}

.potence-divisor-row {
  display: flex;
  gap: 4px;
  height: 48px;
  align-items: center;
}

@media (min-width: 680px) {
  .potence-divisor-row {
    gap: 8px;
    height: 72px;
  }
}

.potence-divisor-line {
  height: 4px;
  background-color: #1e293b;
  border-radius: 2px;
  margin: 4px 0;
  width: 100%;
}

@media (min-width: 680px) {
  .potence-divisor-line {
    height: 6px;
    margin: 8px 0;
  }
}

.potence-quotient-row {
  display: flex;
  gap: 4px;
  height: 48px;
  align-items: center;
}

@media (min-width: 680px) {
  .potence-quotient-row {
    gap: 8px;
    height: 72px;
  }
}

/* Animations */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes scaleUp {
  from { transform: scale(0.96); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes bounceSubtle {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}
.animate-fade-in {
  animation: fadeIn 0.3s ease-out forwards;
}
.animate-scale-up {
  animation: scaleUp 0.25s ease-out forwards;
}
.animate-bounce-subtle {
  animation: bounceSubtle 1.8s ease-in-out infinite;
}

