/* Visual language follows shadcn/ui's default (neutral) theme, written as plain CSS:
   design tokens, 0.5rem radius, hairline borders, solid-fill primary buttons, ring focus. */

:root {
  --background: 0 0% 100%;
  --foreground: 240 10% 3.9%;
  --card: 0 0% 100%;
  --muted: 240 4.8% 95.9%;
  --muted-foreground: 240 3.8% 42%;
  --accent: 240 4.8% 95.9%;
  --border: 240 5.9% 90%;
  --input: 240 5.9% 86%;
  --primary: 240 5.9% 10%;
  --primary-foreground: 0 0% 98%;
  --destructive: 0 72% 46%;
  --ring: 240 5% 34%;
  --radius: 8px;

  --hours: 14;
  --grid-h: clamp(520px, calc(100vh - 330px), 760px);
  --pad: 16px;
  --control-h: 40px;
  --control-fs: 0.875rem;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: hsl(var(--muted) / 0.45);
  color: hsl(var(--foreground));
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 0.9375rem;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
body.sheet-open { overflow: hidden; }

/* touch screens and phones: 44px controls, 16px input text (prevents iOS zoom on focus) */
@media (max-width: 767px), (pointer: coarse) {
  :root { --control-h: 44px; --control-fs: 1rem; }
}

.site-header, main, .site-footer {
  max-width: 1200px;
  margin: 0 auto;
  padding-left: var(--pad);
  padding-right: var(--pad);
}
.site-header { padding-top: 32px; padding-bottom: 4px; }
h1 { margin: 0 0 8px; font-size: 2.25rem; line-height: 1.15; font-weight: 800; letter-spacing: -0.025em; }
.intro { margin: 0; color: hsl(var(--muted-foreground)); max-width: 640px; font-size: 1.0625rem; }
h2 { font-size: 1.125rem; line-height: 1.3; font-weight: 600; letter-spacing: -0.01em; margin: 0 0 16px; }
h3 { font-size: 1rem; font-weight: 600; margin: 24px 0 4px; }

/* Card */
.card {
  background: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: calc(var(--radius) + 4px);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  padding: 24px;
}

/* ---------- layout: sidebar + schedule on wide screens ---------- */
.tool {
  display: grid;
  gap: 16px;
  margin-top: 24px;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "schedule" "list";
}
.schedule { grid-area: schedule; }
.list-card { grid-area: list; }
#course-form { grid-area: form; }

@media (min-width: 1180px) {
  .tool {
    grid-template-columns: 360px minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    grid-template-areas: "form schedule" "list schedule";
    align-items: start;
  }
  .schedule { position: sticky; top: 16px; }
}

/* ---------- form controls ---------- */
.field { display: flex; flex-direction: column; gap: 8px; margin: 0 0 16px; min-width: 0; }
.field > label, legend { font-size: 0.875rem; font-weight: 500; line-height: 1; }
fieldset.field { border: 0; padding: 0; }
legend { padding: 0; margin-bottom: 8px; }

input[type="text"], select {
  font: inherit;
  font-size: var(--control-fs);
  height: var(--control-h);
  padding: 0 12px;
  border: 1px solid hsl(var(--input));
  border-radius: var(--radius);
  background-color: hsl(var(--background));
  color: hsl(var(--foreground));
  width: 100%;
  min-width: 0;
  display: block;
  -webkit-appearance: none;
  appearance: none;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  transition: border-color 0.15s, box-shadow 0.15s;
}
input[type="text"]:hover, select:hover { border-color: hsl(var(--ring) / 0.55); }
select {
  padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%236b6b73' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 16px;
  cursor: pointer;
}
/* Time picker: hour box and minute box on one line, start and end on two lines */
.time-field { margin-bottom: 12px; }
.time-pair { display: grid; grid-template-columns: minmax(0, 3fr) auto minmax(0, 2fr); align-items: center; gap: 8px; }
.time-sep { font-weight: 600; color: hsl(var(--muted-foreground)); }

:focus-visible { outline: 2px solid hsl(var(--ring)); outline-offset: 2px; }
input[type="text"]:focus-visible, select:focus-visible { outline: 0; border-color: hsl(var(--ring)); box-shadow: 0 0 0 3px hsl(var(--ring) / 0.25); }

/* Color swatches: recommended colors + a free-choice picker */
.swatches { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.swatch-btn, .swatch-custom {
  --sw: 30px;
  position: relative;
  flex: none;
  width: var(--sw); height: var(--sw); padding: 0;
  border-radius: 999px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  box-shadow: none;
  cursor: pointer;
  transition: box-shadow 0.12s, transform 0.12s;
}
.swatch-btn:hover, .swatch-custom:hover { transform: scale(1.08); }
.swatch-btn[aria-pressed="true"], .swatch-custom.selected {
  box-shadow: 0 0 0 2px hsl(var(--background)), 0 0 0 4px hsl(var(--primary));
}
.swatch-btn[aria-pressed="true"]::after {
  content: ""; position: absolute; inset: 0;
  background: no-repeat center / 14px url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%231f2937' stroke-width='3.2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E");
}
.swatch-custom {
  display: block;
  background: conic-gradient(#ef4444, #f59e0b, #eab308, #22c55e, #06b6d4, #3b82f6, #a855f7, #ec4899, #ef4444);
}
.swatch-custom.selected { background-image: none; }
.swatch-custom:not(.selected)::after {
  content: ""; position: absolute; inset: 0;
  background: no-repeat center / 14px url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3.2' stroke-linecap='round'%3E%3Cpath d='M12 5v14M5 12h14'/%3E%3C/svg%3E");
  filter: drop-shadow(0 0 1px rgba(0, 0, 0, 0.6));
}
.swatch-custom input {
  position: absolute; inset: 0; width: 100%; height: 100%;
  margin: 0; padding: 0; border: 0; opacity: 0; cursor: pointer;
}
.swatch-custom:focus-within { outline: 2px solid hsl(var(--ring)); outline-offset: 3px; }
@media (max-width: 767px), (pointer: coarse) { .swatch-btn, .swatch-custom { --sw: 40px; } .swatches { gap: 12px; } }

/* Toggle group (days) */
.day-options { display: flex; flex-wrap: wrap; gap: 8px; }
.day-options label { position: relative; display: block; }
.day-options label[hidden] { display: none; }
.day-options input {
  position: absolute; inset: 0; width: 100%; height: 100%;
  opacity: 0; margin: 0; cursor: pointer;
}
.day-options span {
  display: flex; align-items: center; justify-content: center;
  min-width: 52px; height: var(--control-h); padding: 0 14px;
  border: 1px solid hsl(var(--input)); border-radius: var(--radius);
  background: hsl(var(--background)); font-size: 0.875rem; font-weight: 500;
  user-select: none;
  transition: background-color 0.15s, color 0.15s, border-color 0.15s;
}
.day-options label:hover span { background: hsl(var(--accent)); }
.day-options input:checked + span {
  background: hsl(var(--primary)); border-color: hsl(var(--primary)); color: hsl(var(--primary-foreground));
}
.day-options input:focus-visible + span { outline: 2px solid hsl(var(--ring)); outline-offset: 2px; }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }

/* always reserves two lines so showing or clearing a message never moves the layout */
.form-error { color: hsl(var(--destructive)); font-size: 0.875rem; font-weight: 500; line-height: 1.4; min-height: 2.8em; margin: 0 0 8px; }

/* Button */
button {
  font: inherit;
  font-size: var(--control-fs);
  font-weight: 500;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: var(--control-h);
  padding: 0 16px;
  border: 1px solid hsl(var(--input));
  border-radius: var(--radius);
  background: hsl(var(--background));
  color: hsl(var(--foreground));
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 0.15s, color 0.15s, border-color 0.15s;
}
button:hover { background: hsl(var(--accent)); }
button.primary { background: hsl(var(--primary)); border-color: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
button.primary:hover { background: hsl(var(--primary) / 0.9); }
button.danger { color: hsl(var(--destructive)); border-color: hsl(var(--destructive) / 0.35); margin-left: auto; }
button.danger:hover { background: hsl(var(--destructive) / 0.08); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Sheet / Dialog: the form below 1180px */
.fab, .sheet-backdrop { display: none; }
@media (max-width: 1179px) {
  #course-form {
    display: none;
    position: fixed;
    z-index: 30;
    left: 0; right: 0; bottom: 0;
    max-height: 92vh;
    max-height: 92dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    border-radius: 16px 16px 0 0;
    border-bottom: 0;
    padding: 24px var(--pad) calc(24px + env(safe-area-inset-bottom));
    box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.25);
  }
  body.sheet-open #course-form { display: block; }
  #course-form .actions {
    position: sticky; bottom: calc(-24px - env(safe-area-inset-bottom));
    margin: 0 calc(-1 * var(--pad)); padding: 12px var(--pad) calc(12px + env(safe-area-inset-bottom));
    background: hsl(var(--background)); border-top: 1px solid hsl(var(--border));
  }
  .sheet-backdrop {
    position: fixed; inset: 0; z-index: 20;
    background: rgba(0, 0, 0, 0.8);
  }
  body.sheet-open .sheet-backdrop { display: block; }
  .fab {
    display: inline-flex;
    position: fixed; z-index: 10;
    right: calc(16px + env(safe-area-inset-right));
    bottom: calc(16px + env(safe-area-inset-bottom));
    height: 48px; padding: 0 24px;
    border-radius: 999px;
    background: hsl(var(--primary)); border-color: hsl(var(--primary)); color: hsl(var(--primary-foreground));
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 4px 6px -4px rgba(0, 0, 0, 0.2);
  }
  .fab:hover { background: hsl(var(--primary) / 0.9); }
  body.sheet-open .fab { display: none; }
}
@media (min-width: 700px) and (max-width: 1179px) {
  #course-form {
    left: 50%; right: auto; bottom: auto; top: 50%;
    transform: translate(-50%, -50%);
    width: min(560px, calc(100% - 32px));
    max-height: 90vh;
    max-height: 90dvh;
    border-radius: calc(var(--radius) + 4px);
    border-bottom: 1px solid hsl(var(--border));
    padding: 24px;
  }
  #course-form .actions { margin: 0 -24px; padding: 12px 24px; bottom: -24px; }
}
@media (min-width: 1180px) {
  #cancel-btn.add-mode { display: none; }
}

/* ---------- schedule card ---------- */
.schedule { padding: 20px; }
.schedule-head {
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px 16px; flex-wrap: wrap; margin-bottom: 16px;
}
.schedule-head h2 { margin: 0; }
.head-actions { display: flex; align-items: center; gap: 8px 20px; flex-wrap: wrap; }

/* Switch */
.weekend-switch { display: inline-flex; align-items: center; gap: 10px; min-height: var(--control-h); font-size: 0.875rem; font-weight: 500; cursor: pointer; }
input#weekend-toggle {
  appearance: none; -webkit-appearance: none;
  flex: none; margin: 0;
  width: 40px; height: 22px; border-radius: 999px;
  background-color: hsl(var(--input));
  background-image: radial-gradient(circle closest-side, #fff 96%, transparent 100%);
  background-size: 18px 18px;
  background-repeat: no-repeat;
  background-position: 2px center;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.04);
  transition: background-color 0.15s, background-position 0.15s;
  cursor: pointer;
}
input#weekend-toggle:checked { background-color: hsl(var(--primary)); background-position: 20px center; }
input#weekend-toggle:focus-visible { outline: 2px solid hsl(var(--ring)); outline-offset: 2px; }

/* Alert (conflicts) */
.conflict-notes { list-style: none; padding: 0; margin: 0 0 12px; }
.conflict-notes li {
  display: flex; gap: 10px; align-items: flex-start;
  color: hsl(var(--destructive));
  background: hsl(var(--destructive) / 0.05);
  border: 1px solid hsl(var(--destructive) / 0.4);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin: 8px 0 0;
  font-size: 0.875rem; font-weight: 500;
}
.conflict-notes li::before {
  content: "";
  flex: none; width: 16px; height: 16px; margin-top: 3px;
  background: no-repeat center / contain url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23c0262d' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Cline x1='12' y1='8' x2='12' y2='12'/%3E%3Cline x1='12' y1='16' x2='12.01' y2='16'/%3E%3C/svg%3E");
}

/* grid view (768px and up) */
.grid-wrap { display: none; }
@media (min-width: 768px) { .grid-wrap { display: block; } .agenda { display: none !important; } }

.grid {
  display: grid;
  grid-template-columns: 52px repeat(var(--cols, 5), minmax(0, 1fr));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  overflow: hidden;
  background: hsl(var(--background));
}
.g-corner, .g-head { background: hsl(var(--muted) / 0.6); border-bottom: 1px solid hsl(var(--border)); }
.g-head { padding: 10px 2px; text-align: center; font-size: 0.8125rem; font-weight: 500; color: hsl(var(--muted-foreground)); border-left: 1px solid hsl(var(--border)); }
.g-times, .g-col { padding: 10px 0; }
.g-times { position: relative; }
.g-col { border-left: 1px solid hsl(var(--border)); min-width: 0; }
.g-track { position: relative; height: var(--grid-h); }
.g-col .g-track {
  background-image: linear-gradient(hsl(var(--border)) 1px, transparent 1px);
  background-size: 100% calc(100% / var(--hours));
  border-bottom: 1px solid hsl(var(--border));
}
.g-time {
  position: absolute; right: 6px;
  transform: translateY(-50%);
  font-size: 0.72rem; line-height: 1; color: hsl(var(--muted-foreground)); white-space: nowrap;
}

.block {
  position: absolute;
  container-type: size;
  padding: 1px;
  z-index: 1;
}
.block-btn {
  display: flex; flex-direction: column; justify-content: flex-start; align-items: stretch; gap: 0;
  width: 100%; height: 100%;
  min-height: 0; padding: 2px 3px;
  border: 1px solid rgba(0, 0, 0, 0.18);
  border-radius: calc(var(--radius) - 2px);
  box-shadow: none;
  text-align: left; overflow: hidden;
  line-height: 1.2; font-size: 0.78rem; font-weight: 500;
  white-space: normal;
}
.block-btn:hover { filter: brightness(0.96); }
.block .b-name {
  font-weight: 600;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;
  overflow: hidden; overflow-wrap: anywhere;
}
.block .b-time { font-size: 0.64rem; font-weight: 400; opacity: 0.9; letter-spacing: -0.005em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@container (max-width: 72px) {
  .block .b-time { display: none; }
}
@container (max-height: 35px) {
  .block-btn { justify-content: center; padding-top: 0; padding-bottom: 0; }
  .block .b-name { -webkit-line-clamp: 1; }
  .block .b-time { display: none; }
}
@container (min-height: 36px) and (max-height: 51px) {
  .block .b-name { -webkit-line-clamp: 1; }
}
@container (min-height: 84px) {
  .block .b-time { white-space: normal; overflow: visible; }
}
.block:hover, .block:focus-within {
  z-index: 5;
  left: 0 !important; width: 100% !important;
}
.block.conflict .block-btn {
  border: 2px solid hsl(var(--destructive));
  background-image: repeating-linear-gradient(135deg, hsl(var(--destructive) / 0.28) 0, hsl(var(--destructive) / 0.28) 5px, transparent 5px, transparent 10px);
}

/* day-by-day view (under 768px) */
.agenda { display: block; }
.ag-day { margin: 20px 0 0; }
.ag-day:first-child { margin-top: 0; }
.ag-day-name { font-weight: 600; font-size: 0.8125rem; letter-spacing: 0.02em; padding: 0 0 2px; color: hsl(var(--muted-foreground)); }
.ag-item {
  display: flex; align-items: stretch; gap: 0;
  width: 100%; height: auto; min-height: 56px; margin: 8px 0 0; padding: 0;
  text-align: left; overflow: hidden; white-space: normal; font-weight: 400;
  border-radius: var(--radius);
  background: hsl(var(--background));
}
.ag-bar { width: 10px; flex: none; border-right: 1px solid rgba(0, 0, 0, 0.15); }
.ag-body { padding: 8px 12px; min-width: 0; }
.ag-time { font-size: 0.8125rem; color: hsl(var(--muted-foreground)); }
.ag-name { font-weight: 600; overflow-wrap: anywhere; }
.ag-item.conflict { border-color: hsl(var(--destructive)); border-width: 2px; background: hsl(var(--destructive) / 0.05); }

/* ---------- class list ---------- */
.agenda > .empty { margin: 4px 0 0; }
.empty { color: hsl(var(--muted-foreground)); margin: 0; font-size: 0.875rem; }
.empty[hidden] { display: none; }
.class-list { list-style: none; padding: 0; margin: 0; }
.class-item { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-top: 1px solid hsl(var(--border)); flex-wrap: wrap; }
.class-item:first-child { border-top: 0; padding-top: 0; }
.class-item.conflict { border-left: 3px solid hsl(var(--destructive)); padding-left: 10px; }
.swatch { width: 14px; height: 14px; border-radius: 999px; border: 1px solid rgba(0, 0, 0, 0.2); flex: none; }
.info { flex: 1 1 140px; min-width: 0; }
.c-name { font-weight: 600; font-size: 0.9375rem; overflow-wrap: anywhere; }
.c-meta { color: hsl(var(--muted-foreground)); font-size: 0.8125rem; }
.row-actions { display: flex; gap: 2px; margin-left: auto; flex: none; }
.icon-btn {
  --ic: 36px;
  position: relative;
  width: var(--ic); height: var(--ic); padding: 0;
  border: 0; border-radius: var(--radius);
  background: transparent; box-shadow: none;
  color: hsl(var(--muted-foreground));
}
.icon-btn::before {
  content: ""; position: absolute; inset: 0; margin: auto;
  width: 18px; height: 18px;
  background-color: currentColor;
  -webkit-mask: center / contain no-repeat var(--icon);
  mask: center / contain no-repeat var(--icon);
}
.icon-btn:hover { background: hsl(var(--accent)); color: hsl(var(--foreground)); }
.icon-edit { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z'/%3E%3Cpath d='m15 5 4 4'/%3E%3C/svg%3E"); }
.icon-delete { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M3 6h18'/%3E%3Cpath d='M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6'/%3E%3Cpath d='M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2'/%3E%3Cline x1='10' x2='10' y1='11' y2='17'/%3E%3Cline x1='14' x2='14' y1='11' y2='17'/%3E%3C/svg%3E"); }
.icon-delete:hover { background: hsl(var(--destructive) / 0.1); color: hsl(var(--destructive)); }
@media (max-width: 767px), (pointer: coarse) { .icon-btn { --ic: 44px; } }

.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;
}

/* ---------- text sections ---------- */
.text-block { margin: 40px 0; max-width: 720px; }
.text-block p { margin: 8px 0; color: hsl(var(--muted-foreground)); }
.text-block h3 + p { color: hsl(var(--foreground)); opacity: 0.85; }
.text-block ol { padding-left: 22px; margin: 8px 0; color: hsl(var(--muted-foreground)); }
.text-block li { margin: 4px 0; }
.site-footer { padding-top: 12px; padding-bottom: 40px; color: hsl(var(--muted-foreground)); font-size: 0.875rem; border-top: 0; }

@media (max-width: 1179px) {
  .site-footer { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }
}

@media (max-width: 767px) {
  .list-card { display: none; }
}

@media (max-width: 559px) {
  :root { --pad: 14px; }
  h1 { font-size: 1.75rem; }
  .card { padding: 16px; border-radius: calc(var(--radius) + 2px); }
  .schedule { padding: 16px; }
}

@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; } }

/* ---------- print: only the weekly grid ---------- */
@page { margin: 12mm; }
@media print {
  :root { --grid-h: 170mm; }
  body { background: #fff; padding: 0; }
  .no-print, .fab, .sheet-backdrop { display: none !important; }
  main { max-width: none; padding: 0; }
  .tool { display: block; margin: 0; }
  .card { border: 0; padding: 0; position: static; box-shadow: none; }
  .grid-wrap { display: block !important; }
  .grid { border-radius: 0; }
  .block-btn, .g-head, .g-corner, .swatch {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}
