/* ==========================================================================
   1. CSS custom properties
   ========================================================================== */

:root {
  /* Color palette */
  --color-primary: #3b6fa0;
  --color-primary-dark: #2c5480;
  --color-accent: #e8a838;
  --color-bg: #faf8f5;
  --color-surface: #ffffff;
  --color-text: #2d2a26;
  --color-text-muted: #6b6560;
  --color-success: #4a8c6f;
  --color-error: #c0392b;
  --color-border: #d6d1cb;

  /* Spacing scale */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;
  --space-2xl: 3rem;

  /* Typography scale */
  --font-size-sm: 0.875rem;
  --font-size-base: 1rem;
  --font-size-lg: 1.125rem;
  --font-size-xl: 1.375rem;
  --font-size-2xl: 1.75rem;
  --line-height-base: 1.5;

  /* Misc */
  --radius: 0.5rem;
  --shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  --transition: 150ms ease;
}

/* ==========================================================================
   2. Reset / base
   ========================================================================== */

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
  font-size: var(--font-size-base);
  line-height: var(--line-height-base);
  color: var(--color-text);
  background-color: var(--color-bg);
  min-height: 100vh;
  /* Safe area insets for iOS notch / home indicator */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

h1,
h2,
h3 {
  line-height: 1.2;
}

a {
  color: var(--color-primary);
}

ul,
ol {
  list-style: none;
}

/* Respect the HTML `hidden` attribute even when a component sets display */
[hidden] {
  display: none !important;
}

/* Accessibility: visually hidden but available to screen readers */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ==========================================================================
   3. Layout
    ========================================================================== */

:root {
  --header-padding-top: max(var(--space-md), env(safe-area-inset-top));
}

.container {
  width: 100%;
  max-width: 480px;
  margin-inline: auto;
  padding: var(--space-md);
  padding-top: calc(
    var(--header-padding-top) + var(--space-md) + var(--space-sm) + 1.5rem
  );
  display: flex;
  flex-direction: column;
  gap: var(--space-lg);
}

.card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: 0;
}

/* ==========================================================================
   4. Header
   ========================================================================== */

.app-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 50;
  background-color: var(--color-primary);
  color: var(--color-surface);
  padding-top: var(--header-padding-top);
  padding-bottom: var(--space-sm);
  padding-left: var(--space-lg);
  padding-right: var(--space-lg);
  text-align: center;
}

.app-header h1 {
  font-size: var(--font-size-xl);
  font-weight: 700;
  letter-spacing: 0.01em;
}

/* ==========================================================================
   5. Grading key editor
   ========================================================================== */

/* Editor toggle button (wraps the h2) */
.editor-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  width: 100%;
  text-align: left;
  min-height: 44px;
}

.editor-toggle h2 {
  font-size: var(--font-size-lg);
  color: var(--color-primary);
  flex: 1;
}

.chevron {
  display: inline-block;
  color: var(--color-primary);
  font-size: var(--font-size-sm);
  /* Point right (collapsed) by default */
  transform: rotate(0deg);
  transition: transform var(--transition);
  line-height: 1;
  flex-shrink: 0;
}

.chevron--open {
  /* Point down (expanded) */
  transform: rotate(90deg);
}

.result-section {
  gap: var(--space-md);
}

.result-section h2 {
  font-size: var(--font-size-lg);
  color: var(--color-primary);
}

/* New max field */
.field-row {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
  padding-block: var(--space-md);
}

.field-row label {
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.field-row input,
.field-row select {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  min-height: 44px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: var(--font-size-base);
  color: var(--color-text);
  background: var(--color-surface);
  transition: border-color var(--transition);
}

.field-row input:focus,
.field-row select:focus {
  outline: none;
  border-color: var(--color-primary);
}

.field-row input:invalid {
  border-width: 2px;
  border-color: var(--color-error);
}

.field-row input:invalid:focus {
  border-color: var(--color-error);
}

/* Scrollable table container on small screens */
.table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.editor-table,
.result-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--font-size-base);
}

.editor-table th,
.result-table th {
  text-align: center;
  padding: var(--space-xs) var(--space-sm);
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 2px solid var(--color-border);
  width: 33.33%;
}

.editor-table td,
.result-table td {
  padding: var(--space-xs) var(--space-sm);
  border-bottom: 1px solid var(--color-border);
  vertical-align: middle;
  text-align: center;
  width: 33.33%;
}

.editor-table td:first-child,
.result-table td:first-child {
  font-weight: 200;
  font-size: 2rem;
  line-height: 1.6rem;
}

/* Inputs inside the editor table */
.editor-table input[type="number"] {
  width: 100%;
  min-width: 4rem;
  padding: var(--space-xs) var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: calc(var(--radius) / 2);
  font-size: var(--font-size-base);
  color: var(--color-text);
  text-align: center;
  background: var(--color-surface);
  transition: border-color var(--transition);
  /* Minimum touch target */
  min-height: 44px;
}

.editor-table input[type="number"]:focus {
  outline: none;
  border-color: var(--color-primary);
}

.editor-table input[type="number"]:invalid {
  border-width: 2px;
  border-color: var(--color-error);
}

.editor-table input[type="number"]:invalid:focus {
  border-color: var(--color-error);
}

/* Hide number input spinners (optional, keeps it clean) */
.editor-table input[type="number"]::-webkit-inner-spin-button,
.editor-table input[type="number"]::-webkit-outer-spin-button {
  opacity: 0.4;
}

/* Read-only derived cells inside the editor table */
.cell-readonly {
  display: block;
  padding: var(--space-xs) var(--space-sm);
  color: var(--color-text-muted);
  font-size: var(--font-size-base);
  min-height: 44px;
  line-height: 44px;
}

/* ==========================================================================
   6. Result table
   ========================================================================== */

.result-max {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
  font-weight: 600;
}

.result-table td {
  font-size: var(--font-size-lg);
}

/* ==========================================================================
   7. Buttons
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
  padding: var(--space-sm) var(--space-lg);
  border: none;
  border-radius: var(--radius);
  font-size: var(--font-size-base);
  font-weight: 600;
  cursor: pointer;
  transition:
    background-color var(--transition),
    opacity var(--transition);
  /* Minimum touch target */
  min-height: 44px;
  text-decoration: none;
}

.btn:active {
  opacity: 0.85;
}

.btn-primary {
  background-color: var(--color-primary);
  color: var(--color-surface);
}

.btn-primary:hover {
  background-color: var(--color-primary-dark);
}

.btn-secondary {
  background-color: var(--color-surface);
  color: var(--color-primary);
  border: 1.5px solid var(--color-primary);
}

.btn-secondary:hover {
  background-color: var(--color-bg);
}

/* ⓘ fixed info button */
.btn-info {
  position: fixed;
  bottom: calc(var(--space-lg) + env(safe-area-inset-bottom));
  right: var(--space-lg);
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  background-color: var(--color-primary);
  color: var(--color-surface);
  border: none;
  font-size: var(--font-size-xl);
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition);
  z-index: 100;
}

.btn-info:hover {
  background-color: var(--color-primary-dark);
}

/* GitHub link button — fixed, bottom-left */
.btn-github {
  position: fixed;
  bottom: calc(var(--space-lg) + env(safe-area-inset-bottom));
  left: var(--space-lg);
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  background-color: var(--color-primary);
  color: var(--color-surface);
  border: none;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition);
  z-index: 100;
  text-decoration: none;
}

.btn-github:hover {
  background-color: var(--color-primary-dark);
}

.form-actions {
  display: flex;
  gap: var(--space-sm);
  margin-top: var(--space-md);
}

.form-actions .btn {
  flex: 1;
}

/* ==========================================================================
   8. Validation error list
   ========================================================================== */

.error-list {
  list-style: disc;
  padding-left: var(--space-lg);
  color: var(--color-error);
  font-size: var(--font-size-sm);
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

/* ==========================================================================
   9. iOS install guide overlay
   ========================================================================== */

.install-overlay {
  position: fixed;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 200;
  padding: var(--space-md);
  padding-bottom: calc(var(--space-md) + env(safe-area-inset-bottom));
}

.install-overlay__inner {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: var(--space-xl);
  width: 100%;
  max-width: 420px;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.install-overlay h2 {
  font-size: var(--font-size-xl);
  color: var(--color-primary);
}

.install-overlay p {
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
}

.install-steps {
  list-style: decimal;
  padding-left: var(--space-xl);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  font-size: var(--font-size-base);
}

.install-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  display: inline;
  overflow: visible;
}

.install-overlay__close {
  position: absolute;
  top: var(--space-md);
  right: var(--space-md);
  padding: var(--space-xs) var(--space-sm);
  min-height: 44px;
  border: none;
  background: transparent;
  color: var(--color-text-muted);
  font-size: var(--font-size-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  border-radius: var(--radius);
  transition: background-color var(--transition);
  white-space: nowrap;
}

.install-overlay__close:hover {
  background-color: var(--color-bg);
  color: var(--color-text);
}

.install-overlay__actions {
  display: flex;
  justify-content: flex-end;
}

/* Print grid — hidden on screen, shown only in @media print */
.print-grid {
  display: none;
}

/* ==========================================================================
   11. @media print
   ========================================================================== */

@media print {
  @page {
    /* No size: portrait — orientation is the user's choice in the print dialog */
    margin: 15mm;
  }

  .app-header,
  #calc-form,
  .btn-info,
  .btn-github,
  .install-overlay {
    display: none !important;
  }

  body {
    /* Transparent so the print grid table shows through; result-table
       cells have background:#fff to stay opaque over the grid. */
    background-color: transparent !important;
    color: #000;
    padding: 0;
  }

  .container {
    max-width: 100%;
    margin-inline: 0;
    padding: 0 !important;
    gap: 0;
    background-color: transparent !important;
  }

  /* .result-section also carries .card which has padding: var(--space-lg) */
  .card {
    padding: 0 !important;
    box-shadow: none !important;
    border-radius: 0 !important;
    background: transparent !important;
  }

  /* Print grid table: fixed at page origin, covering the full printable area.
     position:fixed maps to the page box in Chromium's print renderer.
     Cells are 5mm×5mm with a hairline border — CSS mm gives exact physical
     spacing on any paper size and orientation. The table is over-provisioned
     (400×550mm); the page boundary clips excess rows and columns.
     z-index:0 sits behind the result table and label (z-index:1). */
  .print-grid {
    display: table;
    position: fixed;
    top: 0;
    left: 0;
    /* Explicit width = 80 cols × 5mm. Required for table-layout:fixed to
       honour the per-cell width:5mm rather than dividing available space. */
    width: 400mm;
    z-index: 0;
    pointer-events: none;
    border-collapse: collapse;
    table-layout: fixed;
  }

  .print-grid td {
    width: 5mm;
    height: 5mm;
    padding: 0;
    border: 0.2pt solid #e8e8e8;
  }

  .result-section {
    display: flex !important;
    flex-direction: column;
    box-shadow: none;
    border-radius: 0;
    padding: 0;
    gap: 4px;
    /* Shrink to content width, top-left */
    width: auto;
    background-color: transparent !important;
  }

  /* Hide the result heading in print output */
  .result-section h2 {
    display: none;
  }

  .result-max {
    font-size: 0.8rem;
    color: #000;
    white-space: nowrap;
    background: transparent;
    margin-bottom: 4px; /* Prevent text clipping by the table below */
    line-height: 1.2;
  }

  /* White background scoped to the text only — the <p> is transparent so
     the grid shows through the empty space to the right of the label. */
  .result-max__label {
    position: relative;
    z-index: 1;
    display: inline-block;
    background: #fff;
    box-shadow: 0 0 0 8px #fff;
  }

  .result-table {
    font-size: 0.8rem;
    width: auto;
  }

  .result-table th,
  .result-table td {
    border-color: #000;
    background: #fff;
    color: #000;
    padding: 2px 6px;
    white-space: nowrap;
    width: auto;
  }

  .form-actions {
    display: none;
  }

  /* Table wrapper must not force full width */
  .result-section .table-wrapper {
    overflow: visible;
  }

  /* position:relative + z-index:1 places the result table above the print
     grid (z-index:0). box-shadow spread creates a white halo without
     affecting layout width, so the grid stays visible to the right. */
  .result-table {
    position: relative;
    z-index: 1;
    box-shadow: 0 0 0 8px #fff;
  }
}

/* ==========================================================================
   12. @media (min-width: 768px) — desktop polish
   ========================================================================== */

@media (min-width: 768px) {
  .app-header h1 {
    font-size: var(--font-size-2xl);
  }

  .container {
    padding: var(--space-xl);
  }

  .card {
    padding: var(--space-xl);
  }
}
