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

:root {
  --bg: #21242e;
  --surface: #1a1d27;
  --surface-2: #22263a;
  --border: #2e3350;
  --accent: #6c8fff;
  --accent-glow: #6c8fff33;
  --text: #e8eaf6;
  --text-muted: #6b7299;
  --radius: 16px;
  --radius-sm: 10px;
  --transition: 180ms ease;
}

[data-theme="light"] {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-2: #eef0f7;
  --border: #d0d4e8;
  --accent: #4a6ef5;
  --accent-glow: #4a6ef522;
  --text: #1a1d2e;
  --text-muted: #7279a0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  min-height: 100dvh;
  display: grid;
  align-items: start;
  justify-items: center;
  padding: 2rem 1rem;
}

.app {
  width: 100%;
  max-width: 760px;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

main {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* Header */
header {
  text-align: center;
  position: relative;
}

.theme-toggle {
  position: absolute;
  top: 0;
  right: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 1rem;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition);
}

.theme-toggle:hover {
  border-color: var(--text-muted);
  color: var(--text);
}

h1 {
  font-size: clamp(1.6rem, 4vw, 2.2rem);
  font-weight: 700;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 0.4rem;
}

/* Section panels */
#inputs-grid,
#results,
#flours-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

#inputs-grid,
#results {
  padding: 0.875rem;
}

.flours-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.875rem;
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  text-align: left;
  transition: color var(--transition);
}

.flours-header:hover {
  color: var(--text);
}

.flours-chevron {
  flex-shrink: 0;
  transition: transform var(--transition);
}

.flours-header[aria-expanded="true"] .flours-chevron {
  transform: rotate(180deg);
}

#flours-content {
  padding: 0 0.875rem 0.875rem;
}

#flours-content:not([hidden]) {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.flours-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

.flour-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition);
}

.flour-btn:hover:not(:disabled) {
  border-color: var(--text-muted);
  color: var(--text);
}

.flour-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* Unit cards */
.converter {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.5rem;
}

.unit-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.unit-card:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.unit-header {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
}

.unit-symbol {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--accent);
  line-height: 1;
}

.unit-name {
  font-size: 0.8rem;
  color: var(--text-muted);
  letter-spacing: 1px;
}

.input-wrapper {
  position: relative;
}

.input-wrapper[data-unit]::after {
  content: attr(data-unit);
  position: absolute;
  right: 0.6rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 0.85rem;
  pointer-events: none;
}

.input-wrapper[data-unit] input[type="number"],
.input-wrapper[data-unit] .result {
  padding-right: 1.8rem;
}

input[type="number"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 1.1rem;
  font-weight: 600;
  padding: 0.35rem 0.6rem;
  outline: none;
  transition: border-color var(--transition);
  /* hide spinner arrows */
  -moz-appearance: textfield;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

input[type="number"]:focus {
  border-color: var(--accent);
}

input[type="number"].invalid {
  border-color: var(--hot);
  color: var(--hot);
}

.unit-info {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.4;
}

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.share-btn {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--accent-glow);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  color: var(--accent);
  font-size: 0.8rem;
  padding: 0.35rem 0.75rem;
  cursor: pointer;
  transition: background var(--transition), box-shadow var(--transition);
}

.share-btn:hover {
  background: #6c8fff22;
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.save-label-input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.8rem;
  padding: 0.35rem 0.6rem;
  outline: none;
  width: 160px;
  transition: border-color var(--transition);
}

.save-label-input:focus {
  border-color: var(--accent);
}

.save-label-input::placeholder {
  color: var(--text-muted);
}

.save-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 0.35rem 0.75rem;
  cursor: pointer;
  transition: border-color var(--transition), color var(--transition);
}

.save-btn:hover {
  border-color: var(--text-muted);
  color: var(--text);
}

/* Saves section */
#saves-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.875rem;
}

.saves-heading {
  font-size: 0.7rem;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.save-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0;
  border-top: 1px solid var(--border);
  cursor: pointer;
}

.save-row:hover .save-label {
  color: var(--accent);
}

.save-row:first-child {
  border-top: none;
  padding-top: 0;
}

.save-label {
  flex: 1;
  font-size: 0.85rem;
  color: var(--text);
}

.save-time {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.save-delete {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0 0.25rem;
  cursor: pointer;
  transition: color var(--transition);
}

.save-delete:hover {
  color: var(--hot);
}

.share-toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(6px);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.8rem;
  padding: 0.5rem 1rem;
  pointer-events: none;
  opacity: 0;
  transition: opacity 180ms ease, transform 180ms ease;
  white-space: nowrap;
}

.share-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.share-toast.error {
  border-color: var(--hot);
  color: var(--hot);
}

/* Autolyse toggle */
.autolyse-toggle {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text-muted);
  user-select: none;
  width: fit-content;
}

.autolyse-toggle input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  width: 36px;
  height: 20px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  position: relative;
  flex-shrink: 0;
  transition: background var(--transition), border-color var(--transition);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform var(--transition), background var(--transition);
}

.autolyse-toggle input:checked ~ .toggle-track {
  background: var(--accent-glow);
  border-color: var(--accent);
}

.autolyse-toggle input:checked ~ .toggle-track .toggle-thumb {
  transform: translateX(16px);
  background: var(--accent);
}

/* Results phases */
#results {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.autolyse-only {
  display: none;
}

#results.autolyse .autolyse-only {
  display: flex;
}

.autolyse-input-only {
  display: none;
}

#inputs-grid.autolyse .autolyse-input-only {
  display: flex;
}

.section-label {
  font-size: 0.7rem;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
}

.phase-group {
  padding-top: 0.75rem;
}

.phase-group:first-child {
  padding-top: 0;
  border-top: none;
}

.phase-label {
  display: none;
  font-size: 0.7rem;
  letter-spacing: 1px;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
}

.first-phase-label {
  display: block;
}

.first-phase-label::before {
  content: "Ingredients";
}

#results.autolyse .first-phase-label::before {
  content: "Before Autolyse";
}

#results.autolyse .phase-label {
  display: block;
}


.result {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  padding: 0.35rem 0.6rem;
  color: var(--accent);
}

@media (max-width: 480px) {
  .converter {
    grid-template-columns: 1fr;
  }

  .toolbar {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .toolbar-right {
    flex-wrap: wrap;
    width: 100%;
  }

  .save-label-input {
    flex: 1;
    min-width: 0;
    width: auto;
  }
}
