/* =============================================================
   CSS DEMO — styles.css
   Each section is labelled with the concept it demonstrates.
   ============================================================= */


/* -------------------------------------------------------------
   1. CUSTOM PROPERTIES (CSS Variables)
   Defined on :root so every element can inherit them.
   Change a value here and it updates everywhere.
------------------------------------------------------------- */
:root {
  --color-bg:        #f8f7f4;
  --color-surface:   #ffffff;
  --color-border:    #e0ddd7;
  --color-text:      #1a1a1a;
  --color-muted:     #6b6b6b;
  --color-primary:   #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-accent:    #f59e0b;
  --color-code-bg:   #f1f0ed;

  --font-sans: system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "Fira Code", monospace;

  --radius:   6px;
  --shadow:   0 1px 4px rgba(0, 0, 0, 0.08);
  --max-width: 780px;
}


/* -------------------------------------------------------------
   2. CSS RESET / BASE
   Browsers ship with a "user-agent stylesheet". This resets
   the parts that vary between browsers.
------------------------------------------------------------- */
*, *::before, *::after {
  box-sizing: border-box; /* padding/border counted inside width */
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth; /* animated jump for anchor links */
}

body {
  background-color: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: 1rem;        /* 16 px by default */
  line-height: 1.7;
  padding: 0 1rem;        /* side gutters on narrow screens */
}


/* -------------------------------------------------------------
   3. TYPOGRAPHY — the cascade in action
   Later rules override earlier ones; more-specific selectors
   win over less-specific ones.
------------------------------------------------------------- */
h1, h2, h3, h4, h5, h6 {
  line-height: 1.25;
  margin-top: 1.75em;
  margin-bottom: 0.4em;
  font-weight: 700;
}

h1 { font-size: 2.25rem; }
h2 { font-size: 1.6rem;  border-bottom: 2px solid var(--color-border); padding-bottom: 0.3em; }
h3 { font-size: 1.2rem;  }
h4 { font-size: 1.05rem; }
h5, h6 { font-size: 1rem; color: var(--color-muted); }

p {
  margin-bottom: 1em;
  max-width: 68ch; /* ~ideal reading line length */
}

strong { font-weight: 700; }
em     { font-style: italic; }

mark {
  background-color: #fef08a;
  padding: 0 2px;
  border-radius: 2px;
}

del { color: var(--color-muted); }
ins { text-decoration-color: green; }

small { font-size: 0.85em; }

abbr[title] {
  text-decoration: underline dotted;
  cursor: help;
}

blockquote {
  border-left: 4px solid var(--color-primary);
  margin: 1.5em 0;
  padding: 0.75em 1.25em;
  background: var(--color-surface);
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--color-muted);
  font-style: italic;
}

blockquote footer {
  margin-top: 0.5em;
  font-style: normal;
  font-size: 0.9em;
}

hr {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 2.5em 0;
}


/* -------------------------------------------------------------
   4. CODE BLOCKS
   font-family switches the typeface; background + padding
   create the "chip" effect.
------------------------------------------------------------- */
code, kbd, samp {
  font-family: var(--font-mono);
  font-size: 0.875em;
}

code {
  background: var(--color-code-bg);
  padding: 0.15em 0.4em;
  border-radius: var(--radius);
}

kbd {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-bottom-width: 2px;
  padding: 0.1em 0.45em;
  border-radius: var(--radius);
}

pre {
  background: #1e1e1e;
  color: #d4d4d4;
  padding: 1.25em 1.5em;
  border-radius: var(--radius);
  overflow-x: auto;        /* horizontal scroll on overflow */
  margin-bottom: 1em;
}

pre code {
  background: none;
  padding: 0;
  font-size: 0.9em;
}


/* -------------------------------------------------------------
   5. LAYOUT — max-width centering
   auto left/right margins push the block to the centre.
------------------------------------------------------------- */
header, main, body > footer {
  max-width: var(--max-width);
  margin-left: auto;
  margin-right: auto;
}


/* -------------------------------------------------------------
   6. HEADER
------------------------------------------------------------- */
header {
  padding: 3rem 0 1.5rem;
  border-bottom: 1px solid var(--color-border);
  margin-bottom: 2rem;
}

header > p {
  color: var(--color-muted);
  font-size: 1.1rem;
  margin-top: 0.25em;
}


/* -------------------------------------------------------------
   7. NAVIGATION — Flexbox
   display: flex puts children side-by-side.
   flex-wrap: wrap lets them drop to the next line on narrow screens.
------------------------------------------------------------- */
nav ul {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 0.5rem;
  list-style: none;
  margin-top: 1rem;
}

nav a {
  display: inline-block;
  padding: 0.3em 0.75em;
  border-radius: 999px;        /* pill shape */
  border: 1px solid var(--color-border);
  text-decoration: none;
  font-size: 0.9rem;
  color: var(--color-primary);
  transition: background-color 0.15s, color 0.15s;
}

/* :hover — applies when the mouse is over the element */
nav a:hover {
  background-color: var(--color-primary);
  color: #fff;
}


/* -------------------------------------------------------------
   8. SECTIONS
------------------------------------------------------------- */
section {
  margin-bottom: 3.5rem;
}


/* -------------------------------------------------------------
   9. LINKS
------------------------------------------------------------- */
a {
  color: var(--color-primary);
}

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

/* :focus-visible — keyboard-focus ring, not shown on click */
a:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 2px;
}


/* -------------------------------------------------------------
   10. LISTS
------------------------------------------------------------- */
ul, ol {
  padding-left: 1.5rem;
  margin-bottom: 1em;
}

li {
  margin-bottom: 0.25em;
}

/* description list */
dl {
  margin-bottom: 1em;
}

dt {
  font-weight: 700;
  margin-top: 0.75em;
}

dd {
  margin-left: 1.5rem;
  color: var(--color-muted);
}


/* -------------------------------------------------------------
   11. FIGURE & IMAGE
------------------------------------------------------------- */
figure {
  margin: 1.5em 0;
  display: inline-block;
}

figure img {
  display: block;
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
}

figcaption {
  font-size: 0.85em;
  color: var(--color-muted);
  margin-top: 0.4em;
}


/* -------------------------------------------------------------
   12. TABLE
   border-collapse merges adjacent cell borders into one line.
------------------------------------------------------------- */
table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1.5em;
  font-size: 0.95rem;
  background: var(--color-surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

caption {
  font-size: 0.9em;
  color: var(--color-muted);
  margin-bottom: 0.5em;
  text-align: left;
}

th, td {
  padding: 0.65em 1em;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}

thead th {
  background: var(--color-primary);
  color: #fff;
  font-weight: 600;
}

/* :nth-child(even) — every second row */
tbody tr:nth-child(even) {
  background: var(--color-code-bg);
}

tbody tr:hover {
  background: #dbeafe;
}

tfoot td {
  font-size: 0.85em;
  color: var(--color-muted);
  font-style: italic;
}


/* -------------------------------------------------------------
   13. FORMS
------------------------------------------------------------- */
fieldset {
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1.25em 1.5em;
  margin-bottom: 1.25em;
  background: var(--color-surface);
}

legend {
  font-weight: 600;
  padding: 0 0.5em;
  color: var(--color-primary);
}

label {
  font-size: 0.9rem;
  font-weight: 500;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="date"],
textarea,
select {
  display: block;
  width: 100%;
  max-width: 28rem;
  margin-top: 0.3em;
  margin-bottom: 0.1em;
  padding: 0.5em 0.75em;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1rem;
  background: var(--color-bg);
  color: var(--color-text);
  transition: border-color 0.15s, box-shadow 0.15s;
}

/* :focus — element has keyboard or click focus */
input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}

textarea {
  resize: vertical;  /* allow only vertical resize */
  min-height: 6rem;
}

input[type="range"] {
  width: 100%;
  max-width: 20rem;
  accent-color: var(--color-primary);
}

input[type="checkbox"],
input[type="radio"] {
  accent-color: var(--color-primary);
}

button {
  padding: 0.55em 1.4em;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s, transform 0.1s;
}

button[type="submit"] {
  background-color: var(--color-primary);
  color: #fff;
  margin-right: 0.5em;
}

button[type="submit"]:hover {
  background-color: var(--color-primary-hover);
}

button[type="reset"] {
  background-color: var(--color-code-bg);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

button[type="reset"]:hover {
  background-color: var(--color-border);
}

/* :active — element is being pressed */
button:active {
  transform: scale(0.97);
}

output {
  font-weight: 700;
  color: var(--color-primary);
}


/* -------------------------------------------------------------
   14. MEDIA ELEMENTS
------------------------------------------------------------- */
audio {
  display: block;
  margin-bottom: 1.5em;
  width: 100%;
  max-width: 28rem;
}

video {
  display: block;
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  margin-bottom: 1.5em;
}

iframe {
  display: block;
  max-width: 100%;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}


/* -------------------------------------------------------------
   15. SEMANTIC ELEMENT HIGHLIGHTS
   article / aside get subtle card treatment so learners can
   see the boundaries that otherwise have no visual form.
------------------------------------------------------------- */
article {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1.25em 1.5em;
  margin-bottom: 1em;
  box-shadow: var(--shadow);
}

aside {
  background: #fffbeb;
  border-left: 4px solid var(--color-accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 1em 1.25em;
  margin-bottom: 1em;
}

details {
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 0.75em 1.25em;
  margin-bottom: 0.75em;
  background: var(--color-surface);
}

details[open] {
  padding-bottom: 1em;
}

summary {
  cursor: pointer;
  font-weight: 600;
  list-style: none; /* hide default triangle in some browsers */
}

/* custom triangle via ::before pseudo-element */
summary::before {
  content: "▶ ";
  font-size: 0.7em;
  color: var(--color-primary);
}

details[open] > summary::before {
  content: "▼ ";
}


/* -------------------------------------------------------------
   16. FOOTER
------------------------------------------------------------- */
body > footer {
  margin-top: 4rem;
  padding: 2rem 0;
  border-top: 1px solid var(--color-border);
  color: var(--color-muted);
  font-size: 0.9rem;
}


/* -------------------------------------------------------------
   17. RESPONSIVE DESIGN — media queries
   Rules inside @media only apply when the condition is true.
   "max-width: 600px" targets screens narrower than 600 px.
------------------------------------------------------------- */
@media (max-width: 600px) {
  h1 { font-size: 1.75rem; }
  h2 { font-size: 1.3rem; }

  fieldset {
    padding: 1em;
  }

  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="date"],
  textarea,
  select {
    max-width: 100%;
  }
}


/* =============================================================
   JS-POWERED FEATURES
   These rules only take effect when JavaScript adds/removes
   classes or attributes — a good illustration of how JS and
   CSS collaborate.
   ============================================================= */


/* -------------------------------------------------------------
   18. DARK MODE
   Triggered by JS setting data-theme="dark" on <html>.
   Overrides the custom properties from :root — every rule
   that uses var(--color-*) updates automatically.
------------------------------------------------------------- */
[data-theme="dark"] {
  --color-bg:        #0f172a;
  --color-surface:   #1e293b;
  --color-border:    #334155;
  --color-text:      #e2e8f0;
  --color-muted:     #94a3b8;
  --color-code-bg:   #1e293b;
  --color-primary:   #60a5fa;
  --color-primary-hover: #93c5fd;
  --shadow:          0 1px 4px rgba(0, 0, 0, 0.4);
}

[data-theme="dark"] aside {
  background: #422006;
}

[data-theme="dark"] tbody tr:hover {
  background: #1e3a5f;
}

/* smooth transition when toggling */
body {
  transition: background-color 0.25s, color 0.25s;
}


/* -------------------------------------------------------------
   19. HEADER LAYOUT (for theme toggle button placement)
------------------------------------------------------------- */
.header-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
}

#theme-toggle {
  flex-shrink: 0;
  margin-top: 0.5rem;
  padding: 0.4em 1em;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  background: var(--color-surface);
  color: var(--color-text);
  font-family: inherit;
  font-size: 0.875rem;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s, border-color 0.15s;
}

#theme-toggle:hover {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}


/* -------------------------------------------------------------
   20. SCROLL SPY — active nav link
   JS adds .active when the linked section enters the viewport.
------------------------------------------------------------- */
nav a.active {
  background-color: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}


/* -------------------------------------------------------------
   21. TABLE SORT INDICATORS
   data-sort attribute is toggled by JS on <th> click.
------------------------------------------------------------- */
thead th {
  cursor: pointer;
  user-select: none;
}

thead th::after {
  content: " ↕";
  opacity: 0.35;
  font-size: 0.8em;
}

thead th[data-sort="asc"]::after  { content: " ↑"; opacity: 1; }
thead th[data-sort="desc"]::after { content: " ↓"; opacity: 1; }


/* -------------------------------------------------------------
   22. COPY CODE BUTTON
   Injected by JS inside each <pre> block.
------------------------------------------------------------- */
pre {
  position: relative;
}

.copy-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  padding: 0.2em 0.65em;
  font-size: 0.75rem;
  font-family: inherit;
  font-weight: 600;
  border: 1px solid #444;
  border-radius: var(--radius);
  background: #2d2d2d;
  color: #ccc;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}

.copy-btn:hover,
.copy-btn.copied {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}


/* -------------------------------------------------------------
   23. CHARACTER COUNTER
   Injected by JS below the <textarea>.
------------------------------------------------------------- */
.char-counter {
  display: block;
  font-size: 0.8rem;
  color: var(--color-muted);
  margin-top: 0.2em;
  margin-bottom: 0.75em;
}

.char-counter.warn {
  color: #d97706;
  font-weight: 600;
}


/* -------------------------------------------------------------
   24. FORM VALIDATION
   .error-msg spans injected by JS; .invalid added to inputs.
------------------------------------------------------------- */
.error-msg {
  display: block;
  color: #dc2626;
  font-size: 0.82rem;
  margin-top: 0.2em;
  margin-bottom: 0.5em;
}

input.invalid,
textarea.invalid {
  border-color: #dc2626;
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.15);
}


/* -------------------------------------------------------------
   25. TOAST NOTIFICATION
   JS creates, appends, and removes these elements.
   The transition from opacity 0 → 1 is the animation.
------------------------------------------------------------- */
.toast {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  background: #166534;
  color: #dcfce7;
  padding: 0.75em 1.25em;
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  opacity: 0;
  transform: translateY(0.5rem);
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
  z-index: 100;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}


/* -------------------------------------------------------------
   26. RESPONSIVE ADDITIONS
------------------------------------------------------------- */
@media (max-width: 600px) {
  .header-top {
    flex-direction: column;
    align-items: flex-start;
  }
}
