/* Inspired by and uses code from 'The Monospace Web' by Oskar Wickström (https://github.com/owickstrom/the-monospace-web) */
@import url('https://fonts.cdnfonts.com/css/jetbrains-mono-2');

/* ==========================================================================
   1. Root Variables, Typography, and Global Resets
   ========================================================================== */

:root {
  --font-family: "JetBrains Mono", monospace;
  --line-height: 1.20rem;
  --font-weight-normal: 500;
  --font-weight-medium: 600;
  --font-weight-bold: 800;
  --font-size-base: 16px;

  --border-thickness: 2px;

  font-family: var(--font-family);
  font-optical-sizing: auto;
  font-weight: var(--font-weight-normal);
  font-style: normal;
  font-variant-numeric: tabular-nums lining-nums;
  font-size: var(--font-size-base);
}

html[data-theme='dark'] {
  --text-color: #fff;
  --text-color-alt: #aaa;
  --background-color: #161616;
  --background-color-alt: #3d3d3d;
}

html[data-theme='light'] {
  --text-color: #000;
  --text-color-alt: #666;
  --background-color: #ebebeb;
  --background-color-alt: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --text-color: #fff;
    --text-color-alt: #aaa;
    --background-color: #161616;
    --background-color-alt: #3d3d3d;
  }
}

@media screen and (max-width: 480px) {
  :root {
    font-size: 14px;
    /* Adjusting base font size for small screens */
  }
}

/* Global Resets */
*,
*::before,
*::after {
  /* Better 'all elements' selector for box-sizing */
  box-sizing: border-box;
}

html {
  display: flex;
  width: 100%;
  margin: 0;
  padding: 0;
  flex-direction: column;
  align-items: center;
  background: var(--background-color);
  color: var(--text-color);
  overflow-y: scroll;
}

body {
  position: relative;
  width: 100%;
  margin: 0;
  padding: var(--line-height) 2ch;
  max-width: calc(min(80ch, round(down, 100%, 1ch)));
  line-height: var(--line-height);
  overflow-x: hidden;
}

@media screen and (max-width: 480px) {
  body {
    padding: var(--line-height) 1ch;
  }
}

/* Text Formatting */
p {
  margin-bottom: var(--line-height);
  word-break: break-word;
  word-wrap: break-word;
  hyphens: auto;
}

strong {
  font-weight: var(--font-weight-bold);
}

em,
figcaption,
img {
  /* Grouped img styling */
  font-style: italic;
}

img {
  color: var(--text-color-alt);
}

sub {
  position: relative;
  display: inline-block;
  margin: 0;
  vertical-align: sub;
  line-height: 0;
  width: calc(1ch / 0.75);
  font-size: .75rem;
}

/* ==========================================================================
   2. Headings and Horizontal Rule
   ========================================================================== */

h1,
h2,
h3,
h4,
h5,
h6 {
  font-weight: var(--font-weight-bold);
  /* Consistent vertical rhythm: 2x top margin, 1x bottom margin */
  margin: calc(var(--line-height) * 2) 0 var(--line-height);
  line-height: var(--line-height);
}

h1 {
  font-size: 2rem;
  line-height: calc(2 * var(--line-height));
  margin-bottom: calc(var(--line-height) * 2);
  text-transform: uppercase;
}

h2 {
  font-size: 1rem;
  text-transform: uppercase;
}

hr {
  position: relative;
  display: block;
  height: var(--line-height);
  margin: calc(var(--line-height) * 1.5) 0;
  border: none;
  color: var(--text-color);
}

hr::after {
  display: block;
  content: "";
  position: absolute;
  top: calc(var(--line-height) / 2 - var(--border-thickness));
  left: 0;
  width: 100%;
  border-top: calc(var(--border-thickness) * 3) double var(--text-color);
  height: 0;
}

/* ==========================================================================
   3. Links and Media
   ========================================================================== */

a {
  text-decoration-thickness: var(--border-thickness);
}

a:link,
a:visited {
  color: var(--text-color);
}

img,
video {
  display: block;
  width: 100%;
  object-fit: contain;
  overflow: hidden;
}

/* ==========================================================================
   4. Lists
   ========================================================================== */

ul,
ol {
  padding: 0;
  margin: 0 0 var(--line-height);
}

ul {
  list-style-type: square;
  padding-left: 2ch;
}

ol {
  list-style-type: none;
  counter-reset: item;
}

/* Nested lists padding */
ol ul,
ol ol,
ul ol,
ul ul {
  padding: 0 0 0 3ch;
  margin: 0;
}

ol li::before {
  content: counters(item, ".") ". ";
  counter-increment: item;
  font-weight: var(--font-weight-medium);
}

li {
  margin: 0;
  padding: 0;
}

li::marker {
  line-height: 0;
}

/* ==========================================================================
   5. Tables
   ========================================================================== */

table {
  position: relative;
  top: calc(var(--line-height) / 2);
  width: calc(round(down, 100%, 1ch));
  border-collapse: collapse;
  margin: 0 0 calc(var(--line-height) * 2);
}

th,
td {
  border: var(--border-thickness) solid var(--text-color);
  /* Consolidated padding calculation */
  padding:
    calc(var(--line-height) / 2) calc(1ch - var(--border-thickness) / 2) calc(var(--line-height) / 2 - var(--border-thickness));
  line-height: var(--line-height);
  vertical-align: top;
  text-align: left;
}

table tbody tr:first-child>* {
  padding-top: calc(var(--line-height) / 2 - var(--border-thickness));
}

th {
  font-weight: 700;
}

/* ==========================================================================
   6. Code and Figures
   ========================================================================== */

pre,
code {
  font-family: var(--font-family);
}

pre {
  white-space: pre;
  overflow-x: auto;
  margin: var(--line-height) 0;
  overflow-y: hidden;
}

code {
  font-weight: var(--font-weight-medium);
}

figure {
  margin: calc(var(--line-height) * 2) 3ch;
  overflow-x: auto;
  overflow-y: hidden;
}

figure pre {
  margin: 0;
}

figcaption {
  margin-top: var(--line-height);
}

/* ==========================================================================
   7. Forms and User Interaction
   ========================================================================== */

/* Base input/button/textarea styling */
input,
button,
textarea {
  border: var(--border-thickness) solid var(--text-color);
  /* Consolidated padding */
  padding:
    calc(var(--line-height) / 2 - var(--border-thickness)) calc(1ch - var(--border-thickness));
  margin-top: calc(1ch - var(--border-thickness));
  font: inherit;
  font-weight: inherit;
  height: calc(var(--line-height) * 2);
  width: auto;
  overflow: visible;
  background: var(--background-color);
  color: var(--text-color);
  line-height: normal;
  /* Resetting font smoothing and appearance */
  -webkit-font-smoothing: inherit;
  -moz-osx-font-smoothing: inherit;
  -webkit-appearance: none;
}

/* Focus state for form elements */
button:focus,
input:focus,
textarea:focus,
.select-wrapper select:focus {
  --border-thickness: 3px;
  outline: none;
  border-color: var(--text-color);
  background: var(--background-color-alt);
}

/* Button specific styling */
button {
  text-transform: uppercase;
  font-weight: var(--font-weight-medium);
  cursor: pointer;
}

button:hover {
  background: var(--background-color-alt);
}

button:active {
  transform: translate(2px, 2px);
}

button::-moz-focus-inner {
  padding: 0;
  border: 0
}

.right-side-btn {
  float: right;
}

/* Input specific styling */
input {
  width: calc(round(down, 100%, 1ch));
}

input[type="number"] {
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

input[type='range'] {
  accent-color: var(--text-color);
  -webkit-appearance: none;
  appearance: none;
  height: calc(var(--line-height) * 2);
  background: var(--background-color);
  border-radius: 2px;
  outline: none;
  cursor: col-resize;
}

/* Checkbox/Radio styling */
input[type=checkbox],
input[type=radio] {
  display: inline-grid;
  place-content: center;
  vertical-align: top;
  width: 2ch;
  height: var(--line-height);
  cursor: pointer;
}

input[type=checkbox]:checked::before,
input[type=radio]:checked::before {
  content: "";
  width: 1ch;
  height: calc(var(--line-height) / 2);
  background: var(--text-color);
}

input[type=radio],
input[type=radio]::before {
  border-radius: 100%;
}

/* Textarea specific styling */
textarea#zplInput {
  width: 100%;
  box-sizing: border-box;
  height: 700px;
  resize: vertical;
}

/* Placeholder styling */
::placeholder {
  color: var(--text-color-alt);
  opacity: 1;
}

::-ms-input-placeholder {
  color: var(--text-color-alt);
}

/* Label specific styling */
label {
  display: block;
  width: calc(round(down, 100%, 1ch));
  height: auto;
  margin-top: var(--line-height);
  line-height: var(--line-height);
  font-weight: var(--font-weight-medium);
  margin: 0;
}

label input {
  width: 100%;
}

/* Select wrapper styling */
.select-wrapper {
  position: relative;
  padding: 0;
  width: 100%;
  max-width: 100%;
  display: inline-block;
}

.select-wrapper select {
  /* Re-consolidating select styles to override generic input styles */
  border: var(--border-thickness) solid var(--text-color);
  background: var(--background-color);
  color: var(--text-color);
  font-family: var(--font-family);
  font-weight: var(--font-weight-normal);
  padding: calc(var(--line-height) / 2 - var(--border-thickness)) 1ch;
  height: calc(var(--line-height) * 2);
  width: 100%;
  box-sizing: border-box;
  appearance: none;
  cursor: pointer;
  border-radius: 0;
  transition: border-color 0.2s ease;
  padding-right: 2.5ch;
  /* Make space on right for arrow */
  margin-top: calc(1ch - var(--border-thickness));
  /* Inherited from input group */
}

.select-wrapper::after {
  content: "▼";
  position: absolute;
  right: 1.25ch;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
  color: var(--text-color);
  font-size: 1rem;
  user-select: none;
}

/* Hidden File Input for accessibility */
.file-input-hidden {
  opacity: 0;
  z-index: -1;
  width: 0px;
  height: 0px;
  overflow: hidden;
  white-space: nowrap;
}

/* ==========================================================================
   8. Components and Layout
   ========================================================================== */

/* Header */
.header {
  margin-bottom: calc(var(--line-height) * 2);
}

.header h1 {
  margin: 0;
}

.header tr td:last-child {
  text-align: right;
}

/* Tree Structure */
.tree,
.tree ul {
  position: relative;
  padding-left: 0;
  list-style-type: none;
  line-height: var(--line-height);
}

.tree ul {
  margin: 0;
}

.tree ul li {
  position: relative;
  padding-left: 1.5ch;
  margin-left: 1.5ch;
  border-left: var(--border-thickness) solid var(--text-color);
}

.tree ul li::before {
  position: absolute;
  display: block;
  top: calc(var(--line-height) / 2);
  left: 0;
  content: "";
  width: 1ch;
  border-bottom: var(--border-thickness) solid var(--text-color);
}

.tree ul li:last-child {
  border-left: none;
}

.tree ul li:last-child::after {
  position: absolute;
  display: block;
  top: 0;
  left: 0;
  content: "";
  height: calc(var(--line-height) / 2);
  border-left: var(--border-thickness) solid var(--text-color);
}

/* Grid Layout */
.grid {
  --grid-cells: 0;
  display: flex;
  gap: 1ch;
  /* Simplified calc for width, assuming no need for round(down) on 100% minus gaps */
  width: 100%;
  margin-bottom: var(--line-height);
}

.grid>*,
.grid>input {
  /* This calculation is highly functional and uses the custom property for cells */
  flex: 0 0 calc(round(down, (100% - (1ch * (var(--grid-cells) - 1))) / var(--grid-cells), 1ch));
}

/* Grid cell count detection using :has() - kept as is for functionality */
.grid:has(> :last-child:nth-child(1)) {
  --grid-cells: 1;
}

.grid:has(> :last-child:nth-child(2)) {
  --grid-cells: 2;
}

.grid:has(> :last-child:nth-child(3)) {
  --grid-cells: 3;
}

.grid:has(> :last-child:nth-child(4)) {
  --grid-cells: 4;
}

.grid:has(> :last-child:nth-child(5)) {
  --grid-cells: 5;
}

.grid:has(> :last-child:nth-child(6)) {
  --grid-cells: 6;
}

.grid:has(> :last-child:nth-child(7)) {
  --grid-cells: 7;
}

.grid:has(> :last-child:nth-child(8)) {
  --grid-cells: 8;
}

.grid:has(> :last-child:nth-child(9)) {
  --grid-cells: 9;
}

/* Details/Summary */
details {
  border: var(--border-thickness) solid var(--text-color);
  padding: calc(var(--line-height) - var(--border-thickness)) 1ch;
  margin-bottom: var(--line-height);
}

summary {
  font-weight: var(--font-weight-medium);
  cursor: pointer;
}

details[open] summary {
  margin-bottom: var(--line-height);
}

details ::marker {
  display: inline-block;
  content: '▶ ';
  margin: 0;
}

details[open] summary::marker {
  content: '▼ ';
}

details ul li::marker { 
  content: '■ ';
}

details :last-child {
  margin-bottom: 0;
}

/* Canvas Panel */
.canvas-panel {
  margin: 0;
  border: var(--border-thickness) solid var(--text-color);
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

#editor-canvas {
  max-width: 100%;
  max-height: 100%;
}

/* ==========================================================================
   9. Output / Utility
   ========================================================================== */

/* Output Container */
.output-container {
  position: relative;
  display: flex;
  margin-top: 0.5rem;
}

.output-textarea {
  width: 100%;
  height: 200px;
  padding: 0.75rem;
  font-family: inherit;
  font-size: 0.9rem;
  background-color: var(--background-color);
  border-radius: 0;
  resize: none;
  line-height: 1.3;
}

/* Copy Button (In Output Container) */
.copy-button {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background-color: var(--background-color);
  font-size: 0.7rem;
  font-weight: bold;
  padding: 0.2rem 0.5rem;
  transition: all 0.1s;
  border: 1px solid var(--text-color-alt);
  color: var(--text-color);
}

.copy-button:hover {
  background-color: var(--background-color-alt);
  color: var(--text-color);
}

/* Toast Notification */
.toast-notification {
  /* Consolidated base styling with form elements */
  border: var(--border-thickness) solid var(--text-color);
  background: var(--background-color);
  color: var(--text-color);
  font-family: var(--font-family);
  font-weight: var(--font-weight-normal);

  /* Specific Toast positioning and animation */
  position: fixed;
  top: 1rem;
  left: 1rem;
  padding: 0.75rem 1.5rem;
  opacity: 0;
  transform: translateY(2rem);
  transition: opacity 0.9s, transform 0.9s;
  pointer-events: none;
}

/* Utility Classes (Used by JavaScript) */
.opacity-100 {
  opacity: 1;
}

.translate-y-0 {
  transform: translateY(0);
}

.opacity-0 {
  opacity: 0;
}

.translate-y-10 {
  transform: translateY(2.5rem);
}

/* Width Helpers */
.width-min {
  width: 0%;
}

.width-auto {
  width: 100%;
}

/* Scrollbar Styling */
::-webkit-scrollbar {
  height: var(--line-height);
}

/* DEBUG UTILITIES */

.debug .debug-grid {
  --color: color-mix(in srgb, var(--text-color) 10%, var(--background-color) 90%);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
  background-image:
    repeating-linear-gradient(var(--color) 0 1px, transparent 1px 100%),
    repeating-linear-gradient(90deg, var(--color) 0 1px, transparent 1px 100%);
  background-size: 1ch var(--line-height);
  margin: 0;
}

.debug .off-grid {
  background: rgba(255, 0, 0, 0.1);
}

.debug-toggle-label {
  text-align: right;
}

.theme-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  width: 3rem;
  height: 3rem;
  background-color: var(--background-color-alt);
  border: var(--border-thickness) solid var(--text-color);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background-color 0.3s ease, transform 0.2s ease;
}

.theme-toggle:hover {
  background-color: var(--background-color-alt);
  transform: scale(1.05);
}

.theme-toggle .icon {
  width: 1.5rem;
  height: 1.5rem;
  fill: var(--text-color);
}

/* Logic to show the correct icon based on the current theme */
html[data-theme='light'] .theme-toggle .moon-icon {
  display: block;
}

html[data-theme='light'] .theme-toggle .sun-icon {
  display: none;
}

html[data-theme='dark'] .theme-toggle .moon-icon {
  display: none;
}

html[data-theme='dark'] .theme-toggle .sun-icon {
  display: block;
}

/* Smooth transition for theme changes */
body {
  transition: background-color 0.9s ease, color 0.9s ease;
}


.no-select {
  /* Prevents text selection */
  user-select: none;
   /* For WebKit browsers (Chrome, Safari) */
  -webkit-user-select: none;
  /* For Firefox */
  -moz-user-select: none;
  /* For Internet Explorer/Edge */
  -ms-user-select: none;
}

.no-select::selection {
   /* Makes highlight color invisible */
  background: transparent;
   /* Keeps the text color unchanged */
  color: inherit;
}

.no-select::-moz-selection {
  background: transparent; /* For Firefox */
  color: inherit;
}