/* Font loaded in HTML for performance */

:root {
  /* --- Minimalist Monochrome Palette --- */
  --background: #FFFFFF;
  --foreground: #000000;
  --muted: #F5F5F5;
  --muted-foreground: #525252;
  --border: #000000;
  --border-light: #E5E5E5;
  --card: #FFFFFF;

  /* --- Typography --- */
  --font-display: 'Playfair Display', Georgia, serif;
  --font-body: 'Source Serif 4', Georgia, serif;
  --font-mono: 'JetBrains Mono', monospace;

  /* --- Spacing & Layout --- */
  --container-max: 75rem;
  /* Slightly wider to accommodate bigger grid */
  --cell-size: 64px;

  /* --- Borders (No Radius) --- */
  --radius: 0px;
  --border-thin: 1px solid var(--border);
  --border-medium: 2px solid var(--border);
  --border-thick: 4px solid var(--border);
}

/* --- Global Reset & Base --- */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-body);
  background-color: var(--background);
  color: var(--foreground);
  line-height: 1.625;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;

  /* Global Texture: Horizontal Lines (Subtle) */
  background-image: repeating-linear-gradient(0deg,
      transparent,
      transparent 3px,
      rgba(0, 0, 0, 0.03) 3px,
      rgba(0, 0, 0, 0.03) 4px);
  background-size: 100% 4px;
}

/* Overlay for Noise Texture (Pseudo-element) */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  opacity: 0.15;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
  pointer-events: none;
}

/* --- Typography System --- */
h1 {
  font-family: var(--font-display);
  font-weight: 900;
  line-height: 0.9;
  letter-spacing: -0.04em;
  color: var(--foreground);
  font-size: clamp(3rem, 10vw, 8rem);
  /* Dramatic scaling */
  margin-bottom: 0.5rem;
  text-transform: uppercase;
}

p.subtitle {
  font-family: var(--font-body);
  font-size: 1.25rem;
  color: var(--muted-foreground);
  margin-bottom: 3rem;
  font-style: italic;
  max-width: 600px;
  margin-left: auto;
  margin-right: auto;
}

/* --- Layout --- */
header {
  text-align: center;
  padding: 4rem 1.5rem 3rem;
  width: 100%;
  border-bottom: var(--border-thick);
  background: var(--background);
  margin-bottom: 3rem;
  position: relative;
  z-index: 10;
}

main {
  width: 100%;
  max-width: var(--container-max);
  padding: 0 1.5rem;
  display: grid;
  grid-template-columns: 1fr;
  gap: 4rem;
  margin-bottom: 4rem;
}

@media (min-width: 1024px) {
  main {
    grid-template-columns: 350px 1fr;
    align-items: start;
    justify-items: center;
    /* Center grid in its column */
  }
}

/* --- Components: Cards --- */
.card {
  background: var(--card);
  border: var(--border-thin);
  padding: 1.5rem;
  margin-bottom: 2rem;
  transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  /* Ensure card sits above background lines */
  position: relative;
  z-index: 1;
  width: 100%;
}

/* --- Components: Buttons --- */
.button-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

button,
select {
  width: 100%;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  padding: 1rem 0.5rem;
  border: var(--border-medium);
  background: transparent;
  color: var(--foreground);
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  /* Natural transition */
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

button:active {
  transform: scale(0.98);
}

button:focus-visible,
select:focus-visible {
  outline: 3px solid var(--foreground);
  outline-offset: 3px;
}

/* Primary Button (Inverted) */
button.primary {
  background: var(--foreground);
  color: var(--background);
  border-color: var(--foreground);
}

button.primary:hover {
  background: var(--background);
  color: var(--foreground);
}

/* Secondary Button (Outline) */
button.secondary {
  background: transparent;
  color: var(--foreground);
}

button.secondary:hover {
  background: var(--foreground);
  color: var(--background);
}

/* Destructive / Clear / Warning buttons */
#btn-clear {
  color: var(--foreground) !important;
  border-style: dashed;
}

#btn-clear:hover {
  background: repeating-linear-gradient(45deg,
      #FFFFFF,
      #FFFFFF 5px,
      #F5F5F5 5px,
      #F5F5F5 10px);
}

/* --- Components: Upload Area --- */
.upload-box {
  display: none;
  border: 1px dashed var(--muted-foreground);
  padding: 3rem 2rem;
  text-align: center;
  cursor: pointer;
  background: var(--muted);
  transition: all 0.2s;
}

.upload-box:hover,
.upload-box.dragover {
  background: #FFFFFF;
  border: 1px solid var(--foreground);
  /* "Texture" feeling */
  background-image: radial-gradient(circle, #000 0.5px, transparent 0.5px);
  background-size: 10px 10px;
}

.upload-icon {
  font-size: 2rem;
  display: block;
  margin-bottom: 1rem;
  filter: grayscale(100%);
}

#uploadedImage {
  width: 100%;
  margin-top: 1rem;
  border: var(--border-medium);
  display: none;
  filter: grayscale(100%) contrast(1.2);
}

/* --- Components: Message Box --- */
#message {
  padding: 1rem;
  border-left: 4px solid var(--foreground);
  font-family: var(--font-mono);
  font-size: 0.8rem;
  background: var(--muted);
}

/* Game States */
#message.success {
  background: var(--foreground);
  color: var(--background);
  border-left: 4px solid var(--background);
  /* Invert */
}

#message.error {
  /* Diagonal stripes for error */
  background: repeating-linear-gradient(45deg,
      #FFFFFF,
      #FFFFFF 5px,
      #000000 5px,
      #000000 6px);
  color: var(--foreground);
  border: 2px solid var(--foreground);
  font-weight: 700;
  text-shadow: 0px 0px 2px white;
  /* Legibility on stripes */
}

/* Hint box */
.hint-section {
  border: 1px solid var(--foreground);
  padding: 1.5rem;
  margin-top: 1.5rem;
  background: #FFFFFF;
  position: relative;
}

.hint-section::before {
  /* Editorial accent: small black square */
  content: "";
  position: absolute;
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 12px;
  height: 12px;
  background: var(--foreground);
}

.technique-name {
  display: block;
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-style: italic;
  font-weight: 700;
  margin-bottom: 0.5rem;
  border-bottom: 1px solid var(--border-light);
  padding-bottom: 0.5rem;
}

/* --- Sudoku Grid --- */
.sudoku-container {
  display: flex;
  justify-content: center;
}

table {
  border-collapse: collapse;
  background: var(--background);
  border: 4px solid var(--foreground);
  /* Reduced from 6px */
  box-shadow: none;
  z-index: 10;
  position: relative;
}

td {
  width: var(--cell-size);
  height: var(--cell-size);
  border: 1px solid #CCCCCC;
  padding: 0;
  position: relative;
  transition: background-color 0.4s ease;
  /* Smooth background fade */
}

/* Major Grid Lines (3x3) */
td:nth-child(3n) {
  border-right: 3px solid var(--foreground);
}

td:last-child {
  border-right: none;
}

tr:nth-child(3n) td {
  border-bottom: 3px solid var(--foreground);
}

tr:last-child td {
  border-bottom: none;
}

input {
  width: 100%;
  height: 100%;
  border: none;
  text-align: center;
  font-size: 1.5rem;
  font-family: var(--font-mono);
  color: var(--foreground);
  background: transparent;
  cursor: pointer;
  border-radius: 0;
  padding: 0;
  margin: 0;
  transition: all 0.2s ease-out;
  /* opacity: 0; REMOVED to prevent vanishing when animation is swapped */
  animation: appear 0.5s cubic-bezier(0.16, 1, 0.3, 1) both;
}

input:focus {
  outline: none;
  background: var(--foreground);
  color: var(--background);
  transform: scale(1.05);
  /* Subtle pop on focus */
  z-index: 2;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* Cell States */
.focused-cell-group {
  background-color: var(--muted);
}

input.locked {
  font-weight: 900;
  background: #E5E5E5;
}

input.solved {
  font-weight: 700;
  color: var(--foreground);
  animation: inputPop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

input.hint {
  /* Dotted underline or background pattern */
  background-image: radial-gradient(#000 1px, transparent 1px);
  background-size: 4px 4px;
}

input.incorrect {
  background: var(--foreground) !important;
  color: var(--background);
  text-decoration: line-through;
  animation: shake 0.4s cubic-bezier(.36, .07, .19, .97) both;
}

input.related {
  background: var(--muted);
}

/* Candidates */
.candidates {
  pointer-events: none;
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--muted-foreground);
  animation: fadeIn 0.3s ease;
}

.candidates span {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Footer */
footer {
  width: 100%;
  text-align: center;
  margin-top: 4rem;
  padding: 3rem;
  border-top: var(--border-thick);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  background: var(--background);
  z-index: 10;
  position: relative;
}

/* Animations */
@keyframes shake {

  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }

  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }

  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }

  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}

@keyframes appear {
  from {
    opacity: 0;
    transform: scale(0.8);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes inputPop {
  0% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.1);
  }

  100% {
    transform: scale(1);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

.pop-in {
  animation: inputPop 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@media (max-width: 768px) {
  :root {
    --cell-size: min(9vw, 42px);
    /* Cap size so it doesn't get huge on tablets, but fits mobile */
  }

  h1 {
    font-size: 3rem;
  }

  p.subtitle {
    font-size: 1rem;
    margin-bottom: 2rem;
  }

  header,
  footer {
    display: none !important;
  }

  main {
    display: flex;
    flex-direction: column;
    padding: 1rem 0.5rem;
    gap: 1.5rem;
    margin-top: 1rem;
  }

  .sudoku-container {
    order: -1;
    display: flex;
    justify-content: center;
    margin-bottom: 0.5rem;
  }

  /* Stack button groups more densely on mobile if needed */
  .button-group {
    gap: 0.5rem;
  }

  button,
  select {
    padding: 0.75rem 0.25rem;
    font-size: 0.8rem;
    /* Slightly larger text for readability */
  }

  /* Prevent iOS zoom on inputs */
  select,
  input[type="text"],
  input[type="number"] {
    font-size: 16px !important;
  }

  /* Adjust Sudoku Grid Borders for smaller screens */
  table {
    border-width: 2px;
  }

  td:nth-child(3n) {
    border-right-width: 2px;
  }

  tr:nth-child(3n) td {
    border-bottom-width: 2px;
  }

  /* Candidates become very small */
  .candidates {
    font-size: 8px;
    line-height: 1;
  }
}

@media (max-width: 400px) {
  :root {
    /* Maximize space on very small screens */
    --container-max: 100%;
  }

  main {
    padding: 0 0.5rem;
  }

  header {
    padding: 1.5rem 0.5rem;
  }

  h1 {
    font-size: 2.5rem;
  }

  .upload-box {
    padding: 2rem 1rem;
  }

  /* Stack buttons in single column if titles are long? 
     For now, 2-col is usually okay, but let's be safe. */
}