#139 ui light mobile: native-first Scheduling & Time

Interactive Day/Week Time-Block Calendar

Interactive 5-day time-blocking studio with click-to-schedule slots, duration editing, focus vs. meeting meters, and mobile day view.

Inspired by Morgen ↗

Download .html
Fullscreen ↗

WHY THIS AESTHETIC IS UNIQUE

Makes time allocation tangible by turning empty hourly cells into instant focus blocks, tracking Deep Work vs. Meetings live, and switching cleanly to a single-day schedule on 390px screens.

BEST FOR

Personal and team time-blocking plannersResource and studio booking calendarsDeep-work and sprint scheduling toolsClient appointment and shift planners

TAGS

COLOR PALETTE (CLICK TO COPY HEX)

TYPOGRAPHY

Display
Inter
Body
Inter
Mono
JetBrains Mono
Scale
11px mono hour axis, 13px bold event titles, 11.5px time range badges, 14px summary meter stats.

Google Fonts Stylesheet ↗

ARCHITECTURE & MOBILE NOTES

LAYOUT

Fixed hour gutter + 5-day CSS Grid columns on desktop; compact Mon–Fri day-pill switcher + single full-width day timeline on <=768px.

INTERACTION

Click any empty hour slot to create a time block; click any existing block to edit its title, category color, duration (30m/60m/90m/120m), or delete it while live Focus vs. Meeting meters recalculate.

MOBILE

Automatically collapses the 5-day grid into a single-day timeline with large Mon–Fri day selector pills so event titles remain completely readable at 390px.

AI AGENT GUARDRAILS

DO

  • Collapse multi-day calendar grids to a single-day view with day selector pills below 768px.
  • Use distinct category colors paired with category labels for Deep Work, Meetings, and Admin.
  • Recalculate daily/weekly time budget meters immediately on block edits.

DON'T

  • Never cram 5 or 7 vertical day columns onto a 390px mobile screen.
  • Avoid requiring precise pixel-edge dragging as the only way to resize a block's duration.
STANDALONE SOURCE · demos/time-block-calendar.html (30.6 KB)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Solstice — Time-Block Studio Calendar</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,400..700;1,9..40,400&family=Instrument+Serif:ital@0;1&display=swap" rel="stylesheet">
<style>
  :root {
    --bg: #f6f4ef;
    --surface: #ffffff;
    --surface-2: #fbfaf7;
    --border: #e5e0d6;
    --border-soft: #efeae1;
    --ink: #1e2124;
    --muted: #666c75;
    --focus: #2a6f5e;
    --focus-bg: #e4f2ed;
    --focus-bd: #8ec5b5;
    --meeting: #c86432;
    --meeting-bg: #fcece4;
    --meeting-bd: #e8a787;
    --admin: #4f5f8f;
    --admin-bg: #eaeef8;
    --admin-bd: #9fb0db;
    --personal: #8e527d;
    --personal-bg: #f5eaf2;
    --personal-bd: #cea2c2;
    --radius: 12px;
    --hour-h: 58px;
  }
  * { box-sizing: border-box; margin: 0; padding: 0; }
  html, body { height: 100%; overflow-x: hidden; }
  body {
    font-family: 'DM Sans', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--ink);
    line-height: 1.4;
    -webkit-font-smoothing: antialiased;
  }
  .app {
    max-width: 1280px;
    margin: 0 auto;
    padding: 16px 20px 28px;
    display: flex;
    flex-direction: column;
    gap: 14px;
  }
  header.topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 14px 18px;
    box-shadow: 0 2px 10px rgba(30, 33, 36, 0.04);
  }
  .brand {
    display: flex;
    align-items: center;
    gap: 12px;
  }
  .brand-mark {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: linear-gradient(135deg, #2a6f5e, #489782);
    display: grid;
    place-items: center;
    color: #fff;
    font-family: 'Instrument Serif', Georgia, serif;
    font-size: 22px;
    flex-shrink: 0;
  }
  .brand h1 {
    font-family: 'Instrument Serif', Georgia, serif;
    font-weight: 400;
    font-size: 24px;
    letter-spacing: -0.01em;
    line-height: 1.1;
  }
  .brand p {
    font-size: 12.5px;
    color: var(--muted);
  }
  .meter-wrap {
    display: flex;
    align-items: center;
    gap: 16px;
    flex-wrap: wrap;
  }
  .meter-box {
    min-width: 240px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 8px 12px;
  }
  .meter-labels {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    font-weight: 600;
    margin-bottom: 6px;
    gap: 10px;
  }
  .meter-labels .f { color: var(--focus); }
  .meter-labels .m { color: var(--meeting); }
  .meter-labels .o { color: var(--admin); }
  .meter-bar {
    height: 8px;
    border-radius: 99px;
    background: var(--border-soft);
    overflow: hidden;
    display: flex;
  }
  .meter-seg { height: 100%; transition: width 0.25s ease; }
  .meter-seg.focus { background: var(--focus); }
  .meter-seg.meeting { background: var(--meeting); }
  .meter-seg.other { background: var(--admin); }
  .header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
  }
  .btn {
    min-height: 44px;
    padding: 0 14px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: background 0.15s, border-color 0.15s, transform 0.1s;
  }
  .btn:hover { background: var(--surface-2); border-color: #cfc8ba; }
  .btn:active { transform: scale(0.98); }
  .btn-primary {
    background: var(--ink);
    color: #fff;
    border-color: var(--ink);
  }
  .btn-primary:hover { background: #30353a; border-color: #30353a; }
  .btn-danger {
    color: #b42318;
    border-color: #f0c2be;
    background: #fff5f4;
  }
  .btn-danger:hover { background: #fde6e4; }

  .toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }
  .legend {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }
  .chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12.5px;
    font-weight: 500;
    padding: 6px 11px;
    border-radius: 99px;
    background: var(--surface);
    border: 1px solid var(--border);
    cursor: pointer;
    min-height: 36px;
    transition: all 0.15s;
  }
  .chip.active {
    background: var(--ink);
    color: #fff;
    border-color: var(--ink);
  }
  .dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    flex-shrink: 0;
  }
  .hint {
    font-size: 12.5px;
    color: var(--muted);
  }

  /* Mobile day switcher */
  .day-switcher {
    display: none;
    grid-template-columns: repeat(5, 1fr);
    gap: 6px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 6px;
  }
  .day-tab {
    min-height: 48px;
    border: none;
    background: transparent;
    border-radius: 10px;
    font: inherit;
    color: var(--muted);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    transition: background 0.15s, color 0.15s;
  }
  .day-tab strong { font-size: 14px; color: var(--ink); }
  .day-tab span { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; }
  .day-tab.active {
    background: var(--ink);
    color: #fff;
  }
  .day-tab.active strong { color: #fff; }

  /* Calendar board */
  .board {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 4px 18px rgba(30, 33, 36, 0.04);
  }
  .board-head {
    display: grid;
    grid-template-columns: 64px repeat(5, 1fr);
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
  }
  .tz-cell {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 600;
    color: var(--muted);
    border-right: 1px solid var(--border-soft);
    padding: 10px 4px;
  }
  .day-head {
    padding: 10px 12px;
    border-right: 1px solid var(--border-soft);
    display: flex;
    align-items: baseline;
    justify-content: space-between;
  }
  .day-head:last-child { border-right: none; }
  .day-head .dow {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    font-weight: 600;
  }
  .day-head .num {
    font-size: 18px;
    font-weight: 700;
    margin-left: 6px;
  }
  .day-head.today .num {
    background: var(--focus);
    color: #fff;
    padding: 1px 8px;
    border-radius: 99px;
    font-size: 15px;
  }
  .day-head .day-hrs {
    font-size: 11.5px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
  }

  .board-body {
    display: grid;
    grid-template-columns: 64px repeat(5, 1fr);
    position: relative;
  }
  .time-col {
    border-right: 1px solid var(--border-soft);
    background: var(--surface-2);
  }
  .time-label {
    height: var(--hour-h);
    padding: 4px 8px 0 0;
    text-align: right;
    font-size: 11px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
    border-bottom: 1px solid var(--border-soft);
  }
  .day-col {
    position: relative;
    border-right: 1px solid var(--border-soft);
    min-height: calc(var(--hour-h) * 10);
  }
  .day-col:last-child { border-right: none; }
  .slot {
    height: var(--hour-h);
    border-bottom: 1px solid var(--border-soft);
    cursor: pointer;
    position: relative;
    transition: background 0.12s;
  }
  .slot::after {
    content: "";
    position: absolute;
    left: 0; right: 0; top: 50%;
    border-top: 1px dashed #f1ede6;
    pointer-events: none;
  }
  .slot:hover { background: #f8f6f0; }
  .slot:hover::before {
    content: "+ Add block";
    position: absolute;
    right: 8px;
    top: 6px;
    font-size: 11px;
    color: var(--muted);
    font-weight: 500;
  }

  .now-line {
    position: absolute;
    left: 0;
    right: 0;
    height: 2px;
    background: #d9483b;
    z-index: 4;
    pointer-events: none;
  }
  .now-line::before {
    content: "";
    position: absolute;
    left: -4px;
    top: -3px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #d9483b;
  }

  .block {
    position: absolute;
    left: 5px;
    right: 5px;
    border-radius: 10px;
    padding: 6px 9px;
    border: 1px solid transparent;
    border-left-width: 4px;
    cursor: pointer;
    overflow: hidden;
    z-index: 3;
    display: flex;
    flex-direction: column;
    gap: 2px;
    transition: box-shadow 0.15s, transform 0.12s, opacity 0.15s;
    user-select: none;
  }
  .block:hover {
    box-shadow: 0 6px 16px rgba(30, 33, 36, 0.12);
    z-index: 5;
  }
  .block.dimmed { opacity: 0.24; }
  .block.focus { background: var(--focus-bg); border-color: var(--focus-bd); border-left-color: var(--focus); color: #164439; }
  .block.meeting { background: var(--meeting-bg); border-color: var(--meeting-bd); border-left-color: var(--meeting); color: #6e3113; }
  .block.admin { background: var(--admin-bg); border-color: var(--admin-bd); border-left-color: var(--admin); color: #243055; }
  .block.personal { background: var(--personal-bg); border-color: var(--personal-bd); border-left-color: var(--personal); color: #522847; }
  .block-title {
    font-size: 12.5px;
    font-weight: 700;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .block-meta {
    font-size: 11px;
    opacity: 0.82;
    font-variant-numeric: tabular-nums;
    display: flex;
    justify-content: space-between;
    gap: 4px;
  }
  .block-tag {
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    opacity: 0.78;
  }
  .resize-handle {
    position: absolute;
    left: 10px;
    right: 10px;
    bottom: 2px;
    height: 10px;
    cursor: ns-resize;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .resize-handle::after {
    content: "";
    width: 24px;
    height: 3px;
    border-radius: 99px;
    background: currentColor;
    opacity: 0.3;
  }

  /* Modal */
  .modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(20, 22, 26, 0.45);
    backdrop-filter: blur(3px);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 16px;
    z-index: 50;
  }
  .modal-backdrop.open { display: flex; }
  .modal {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 20px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    gap: 14px;
  }
  .modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .modal-header h2 {
    font-family: 'Instrument Serif', Georgia, serif;
    font-weight: 400;
    font-size: 24px;
  }
  .icon-btn {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    font-size: 18px;
    cursor: pointer;
    color: var(--ink);
  }
  .field { display: flex; flex-direction: column; gap: 6px; }
  .field label {
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }
  .field input, .field select {
    min-height: 44px;
    padding: 0 12px;
    border-radius: 10px;
    border: 1px solid var(--border);
    font: inherit;
    font-size: 14px;
    background: var(--surface-2);
    color: var(--ink);
  }
  .row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
  .cat-picker {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }
  .cat-opt {
    min-height: 44px;
    border-radius: 10px;
    border: 1.5px solid var(--border);
    background: var(--surface);
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 12px;
    color: var(--ink);
  }
  .cat-opt.selected { border-color: var(--ink); background: var(--surface-2); }
  .modal-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-top: 4px;
  }
  .modal-footer .right { display: flex; gap: 8px; margin-left: auto; }

  @media (max-width: 820px) {
    .app { padding: 12px 12px 24px; }
    header.topbar { padding: 12px 14px; }
    .meter-box { width: 100%; min-width: 0; }
    .day-switcher { display: grid; }
    .board-head { grid-template-columns: 52px 1fr; }
    .board-body { grid-template-columns: 52px 1fr; }
    .day-head:not(.mobile-active),
    .day-col:not(.mobile-active) { display: none; }
    .hint { display: none; }
  }
</style>
</head>
<body>
<div class="app">
  <header class="topbar">
    <div class="brand">
      <div class="brand-mark">S</div>
      <div>
        <h1>Solstice Studio</h1>
        <p>Week of May 11 – 15 · Product Design Sprint</p>
      </div>
    </div>
    <div class="meter-wrap">
      <div class="meter-box" aria-live="polite">
        <div class="meter-labels">
          <span class="f" id="focusStat">Focus 0h</span>
          <span class="m" id="meetingStat">Meetings 0h</span>
          <span class="o" id="otherStat">Other 0h</span>
        </div>
        <div class="meter-bar">
          <div class="meter-seg focus" id="barFocus" style="width:0%"></div>
          <div class="meter-seg meeting" id="barMeeting" style="width:0%"></div>
          <div class="meter-seg other" id="barOther" style="width:0%"></div>
        </div>
      </div>
      <div class="header-actions">
        <button class="btn" id="autoProtectBtn" title="Add morning focus blocks to open days">Protect Focus</button>
        <button class="btn btn-primary" id="newBlockBtn">+ New Block</button>
      </div>
    </div>
  </header>

  <div class="toolbar">
    <div class="legend" id="filterBar">
      <button class="chip active" data-filter="all">All Blocks</button>
      <button class="chip" data-filter="focus"><span class="dot" style="background:var(--focus)"></span>Deep Focus</button>
      <button class="chip" data-filter="meeting"><span class="dot" style="background:var(--meeting)"></span>Meetings</button>
      <button class="chip" data-filter="admin"><span class="dot" style="background:var(--admin)"></span>Ops &amp; Review</button>
      <button class="chip" data-filter="personal"><span class="dot" style="background:var(--personal)"></span>Recharge</button>
    </div>
    <div class="hint">Click any open hour to add · Click a block to edit · Drag bottom edge to resize</div>
  </div>

  <div class="day-switcher" id="daySwitcher" role="tablist" aria-label="Select day"></div>

  <section class="board" aria-label="Weekly time-block calendar">
    <div class="board-head" id="boardHead"></div>
    <div class="board-body" id="boardBody"></div>
  </section>
</div>

<div class="modal-backdrop" id="modalBackdrop" role="dialog" aria-modal="true" aria-labelledby="modalTitle">
  <form class="modal" id="blockForm">
    <div class="modal-header">
      <h2 id="modalTitle">Edit Time Block</h2>
      <button type="button" class="icon-btn" id="closeModalBtn" aria-label="Close">&times;</button>
    </div>
    <div class="field">
      <label for="fTitle">Block Title</label>
      <input id="fTitle" type="text" required placeholder="e.g., Design system audit">
    </div>
    <div class="row-2">
      <div class="field">
        <label for="fDay">Day</label>
        <select id="fDay"></select>
      </div>
      <div class="field">
        <label for="fStart">Start Time</label>
        <select id="fStart"></select>
      </div>
    </div>
    <div class="row-2">
      <div class="field">
        <label for="fDuration">Duration</label>
        <select id="fDuration">
          <option value="0.5">30 minutes</option>
          <option value="1">1 hour</option>
          <option value="1.5">1.5 hours</option>
          <option value="2">2 hours</option>
          <option value="2.5">2.5 hours</option>
          <option value="3">3 hours</option>
        </select>
      </div>
      <div class="field">
        <label for="fNote">Context / Location</label>
        <input id="fNote" type="text" placeholder="Studio A, Figma, Zoom...">
      </div>
    </div>
    <div class="field">
      <label>Category</label>
      <div class="cat-picker" id="catPicker">
        <button type="button" class="cat-opt" data-cat="focus"><span class="dot" style="background:var(--focus)"></span>Deep Focus</button>
        <button type="button" class="cat-opt" data-cat="meeting"><span class="dot" style="background:var(--meeting)"></span>Meeting</button>
        <button type="button" class="cat-opt" data-cat="admin"><span class="dot" style="background:var(--admin)"></span>Ops &amp; Review</button>
        <button type="button" class="cat-opt" data-cat="personal"><span class="dot" style="background:var(--personal)"></span>Recharge</button>
      </div>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-danger" id="deleteBtn">Delete</button>
      <div class="right">
        <button type="button" class="btn" id="cancelBtn">Cancel</button>
        <button type="submit" class="btn btn-primary">Save Block</button>
      </div>
    </div>
  </form>
</div>

<script>
(() => {
  const DAYS = [
    { id: 0, short: 'Mon', label: 'Monday', date: 11 },
    { id: 1, short: 'Tue', label: 'Tuesday', date: 12, today: true },
    { id: 2, short: 'Wed', label: 'Wednesday', date: 13 },
    { id: 3, short: 'Thu', label: 'Thursday', date: 14 },
    { id: 4, short: 'Fri', label: 'Friday', date: 15 }
  ];
  const START_HOUR = 8;
  const END_HOUR = 18;
  const HOUR_H = 58;
  const CAT_LABELS = { focus: 'Focus', meeting: 'Meeting', admin: 'Ops', personal: 'Recharge' };

  let nextId = 100;
  let blocks = [
    { id: 1, day: 0, start: 8.5, duration: 2, title: 'Onboarding Flow Wireframes', cat: 'focus', note: 'Figma · Quiet mode' },
    { id: 2, day: 0, start: 11, duration: 1, title: 'Sprint Kickoff Sync', cat: 'meeting', note: 'Room 4B / Meet' },
    { id: 3, day: 0, start: 13.5, duration: 1.5, title: 'Token Architecture Spec', cat: 'focus', note: 'Notion' },
    { id: 4, day: 0, start: 16, duration: 1, title: 'PR & Copy Triage', cat: 'admin', note: 'GitHub' },

    { id: 5, day: 1, start: 8.5, duration: 2.5, title: 'Interactive Prototype Build', cat: 'focus', note: 'No Slack' },
    { id: 6, day: 1, start: 11.5, duration: 1, title: 'Design Critique', cat: 'meeting', note: 'Core Product' },
    { id: 7, day: 1, start: 13, duration: 0.5, title: 'Walk + Espresso', cat: 'personal', note: 'Away from desk' },
    { id: 8, day: 1, start: 14, duration: 1.5, title: 'Customer Interview #4', cat: 'meeting', note: 'Dovetail' },
    { id: 9, day: 1, start: 16, duration: 1, title: 'Synthesis Notes', cat: 'admin', note: 'Linear' },

    { id: 10, day: 2, start: 9, duration: 2.5, title: 'Responsive Grid Refactor', cat: 'focus', note: 'Deep Work' },
    { id: 11, day: 2, start: 13, duration: 1, title: 'Eng Handoff Pairing', cat: 'meeting', note: 'With Priya' },
    { id: 12, day: 2, start: 15, duration: 2, title: 'Accessibility Contrast Audit', cat: 'focus', note: 'WCAG AA' },

    { id: 13, day: 3, start: 9.5, duration: 1, title: 'Roadmap Check-in', cat: 'meeting', note: 'Product Leads' },
    { id: 14, day: 3, start: 11, duration: 2, title: 'Motion Timing Curves', cat: 'focus', note: 'Rive + CSS' },
    { id: 15, day: 3, start: 14.5, duration: 1, title: 'Analytics Funnel Review', cat: 'admin', note: 'PostHog' },
    { id: 16, day: 3, start: 16, duration: 1, title: '1:1 with Elena', cat: 'meeting', note: 'Coffee walk' },

    { id: 17, day: 4, start: 8.5, duration: 2, title: 'Component QA Polish', cat: 'focus', note: 'Storybook' },
    { id: 18, day: 4, start: 11.5, duration: 1, title: 'Weekly Demo Showcase', cat: 'meeting', note: 'All-hands' },
    { id: 19, day: 4, start: 14, duration: 1, title: 'Inbox Zero & Next Week', cat: 'admin', note: 'Planning' },
    { id: 20, day: 4, start: 15.5, duration: 1, title: 'Gym / Reset', cat: 'personal', note: 'Off early' }
  ];

  let activeDay = 1;
  let activeFilter = 'all';
  let editingId = null;
  let selectedCat = 'focus';

  const boardHead = document.getElementById('boardHead');
  const boardBody = document.getElementById('boardBody');
  const daySwitcher = document.getElementById('daySwitcher');
  const modalBackdrop = document.getElementById('modalBackdrop');
  const blockForm = document.getElementById('blockForm');
  const fTitle = document.getElementById('fTitle');
  const fDay = document.getElementById('fDay');
  const fStart = document.getElementById('fStart');
  const fDuration = document.getElementById('fDuration');
  const fNote = document.getElementById('fNote');
  const deleteBtn = document.getElementById('deleteBtn');
  const modalTitle = document.getElementById('modalTitle');

  function fmtHour(h) {
    const whole = Math.floor(h);
    const mins = (h % 1) ? '30' : '00';
    const suffix = whole >= 12 ? 'PM' : 'AM';
    const hr12 = whole > 12 ? whole - 12 : (whole === 0 ? 12 : whole);
    return `${hr12}:${mins} ${suffix}`;
  }
  function fmtShort(h) {
    const whole = Math.floor(h);
    const mins = (h % 1) ? ':30' : '';
    const hr12 = whole > 12 ? whole - 12 : whole;
    return `${hr12}${mins}${whole >= 12 ? 'p' : 'a'}`;
  }

  // Populate modal selects
  DAYS.forEach(d => {
    const opt = document.createElement('option');
    opt.value = d.id;
    opt.textContent = `${d.label}, May ${d.date}`;
    fDay.appendChild(opt);
  });
  for (let h = START_HOUR; h < END_HOUR; h += 0.5) {
    const opt = document.createElement('option');
    opt.value = h;
    opt.textContent = fmtHour(h);
    fStart.appendChild(opt);
  }

  function updateMeters() {
    let focus = 0, meeting = 0, other = 0;
    blocks.forEach(b => {
      if (b.cat === 'focus') focus += b.duration;
      else if (b.cat === 'meeting') meeting += b.duration;
      else other += b.duration;
    });
    const total = Math.max(focus + meeting + other, 0.1);
    document.getElementById('focusStat').textContent = `Focus ${focus}h`;
    document.getElementById('meetingStat').textContent = `Meetings ${meeting}h`;
    document.getElementById('otherStat').textContent = `Other ${other}h`;
    document.getElementById('barFocus').style.width = `${(focus / total) * 100}%`;
    document.getElementById('barMeeting').style.width = `${(meeting / total) * 100}%`;
    document.getElementById('barOther').style.width = `${(other / total) * 100}%`;
  }

  function renderSwitcher() {
    daySwitcher.innerHTML = '';
    DAYS.forEach(d => {
      const btn = document.createElement('button');
      btn.type = 'button';
      btn.className = 'day-tab' + (d.id === activeDay ? ' active' : '');
      btn.setAttribute('role', 'tab');
      btn.setAttribute('aria-selected', d.id === activeDay ? 'true' : 'false');
      btn.innerHTML = `<span>${d.short}</span><strong>${d.date}</strong>`;
      btn.addEventListener('click', () => {
        activeDay = d.id;
        render();
      });
      daySwitcher.appendChild(btn);
    });
  }

  function render() {
    renderSwitcher();
    updateMeters();

    // Head
    boardHead.innerHTML = '<div class="tz-cell">PDT</div>';
    DAYS.forEach(d => {
      const hrs = blocks.filter(b => b.day === d.id).reduce((s, b) => s + b.duration, 0);
      const cell = document.createElement('div');
      cell.className = `day-head${d.today ? ' today' : ''}${d.id === activeDay ? ' mobile-active' : ''}`;
      cell.innerHTML = `<div><span class="dow">${d.short}</span><span class="num">${d.date}</span></div><span class="day-hrs">${hrs}h</span>`;
      boardHead.appendChild(cell);
    });

    // Body
    boardBody.innerHTML = '';
    const timeCol = document.createElement('div');
    timeCol.className = 'time-col';
    for (let h = START_HOUR; h < END_HOUR; h++) {
      const lbl = document.createElement('div');
      lbl.className = 'time-label';
      lbl.textContent = `${h > 12 ? h - 12 : h} ${h >= 12 ? 'PM' : 'AM'}`;
      timeCol.appendChild(lbl);
    }
    boardBody.appendChild(timeCol);

    DAYS.forEach(d => {
      const col = document.createElement('div');
      col.className = `day-col${d.id === activeDay ? ' mobile-active' : ''}`;
      col.dataset.day = d.id;

      for (let h = START_HOUR; h < END_HOUR; h++) {
        const slot = document.createElement('div');
        slot.className = 'slot';
        slot.addEventListener('click', () => openModal(null, { day: d.id, start: h }));
        col.appendChild(slot);
      }

      if (d.today) {
        const nowLine = document.createElement('div');
        nowLine.className = 'now-line';
        nowLine.style.top = `${(11.25 - START_HOUR) * HOUR_H}px`;
        col.appendChild(nowLine);
      }

      blocks.filter(b => b.day === d.id).forEach(b => {
        const el = document.createElement('div');
        const dimmed = activeFilter !== 'all' && b.cat !== activeFilter;
        el.className = `block ${b.cat}${dimmed ? ' dimmed' : ''}`;
        el.style.top = `${(b.start - START_HOUR) * HOUR_H + 2}px`;
        el.style.height = `${Math.max(b.duration * HOUR_H - 4, 28)}px`;
        el.innerHTML = `
          <div class="block-title">${escapeHtml(b.title)}</div>
          <div class="block-meta">
            <span>${fmtShort(b.start)}–${fmtShort(b.start + b.duration)}${b.note ? ' · ' + escapeHtml(b.note) : ''}</span>
            <span class="block-tag">${CAT_LABELS[b.cat]}</span>
          </div>
          <div class="resize-handle" title="Drag to resize"></div>
        `;
        el.addEventListener('click', (e) => {
          if (e.target.classList.contains('resize-handle')) return;
          e.stopPropagation();
          openModal(b);
        });

        const handle = el.querySelector('.resize-handle');
        handle.addEventListener('pointerdown', (e) => {
          e.stopPropagation();
          e.preventDefault();
          const startY = e.clientY;
          const origDur = b.duration;
          handle.setPointerCapture(e.pointerId);
          const onMove = (ev) => {
            const deltaHrs = Math.round(((ev.clientY - startY) / HOUR_H) * 2) / 2;
            const maxDur = END_HOUR - b.start;
            b.duration = Math.max(0.5, Math.min(maxDur, origDur + deltaHrs));
            el.style.height = `${Math.max(b.duration * HOUR_H - 4, 28)}px`;
            el.querySelector('.block-meta span').textContent = `${fmtShort(b.start)}–${fmtShort(b.start + b.duration)}${b.note ? ' · ' + b.note : ''}`;
            updateMeters();
          };
          const onUp = () => {
            handle.removeEventListener('pointermove', onMove);
            handle.removeEventListener('pointerup', onUp);
            render();
          };
          handle.addEventListener('pointermove', onMove);
          handle.addEventListener('pointerup', onUp);
        });

        col.appendChild(el);
      });

      boardBody.appendChild(col);
    });
  }

  function escapeHtml(str) {
    return str.replace(/[&<>"']/g, c => ({ '&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;' }[c]));
  }

  function setCategory(cat) {
    selectedCat = cat;
    document.querySelectorAll('.cat-opt').forEach(btn => {
      btn.classList.toggle('selected', btn.dataset.cat === cat);
    });
  }

  function openModal(block, defaults = {}) {
    if (block) {
      editingId = block.id;
      modalTitle.textContent = 'Edit Time Block';
      fTitle.value = block.title;
      fDay.value = block.day;
      fStart.value = block.start;
      fDuration.value = block.duration;
      fNote.value = block.note || '';
      setCategory(block.cat);
      deleteBtn.style.display = 'inline-flex';
    } else {
      editingId = null;
      modalTitle.textContent = 'New Time Block';
      fTitle.value = '';
      fDay.value = defaults.day ?? activeDay;
      fStart.value = defaults.start ?? 9;
      fDuration.value = 1;
      fNote.value = '';
      setCategory('focus');
      deleteBtn.style.display = 'none';
    }
    modalBackdrop.classList.add('open');
    setTimeout(() => fTitle.focus(), 40);
  }

  function closeModal() {
    modalBackdrop.classList.remove('open');
  }

  document.getElementById('catPicker').addEventListener('click', (e) => {
    const btn = e.target.closest('.cat-opt');
    if (btn) setCategory(btn.dataset.cat);
  });

  blockForm.addEventListener('submit', (e) => {
    e.preventDefault();
    const start = parseFloat(fStart.value);
    const dur = Math.min(parseFloat(fDuration.value), END_HOUR - start);
    const payload = {
      title: fTitle.value.trim() || 'Untitled Block',
      day: parseInt(fDay.value, 10),
      start,
      duration: Math.max(0.5, dur),
      cat: selectedCat,
      note: fNote.value.trim()
    };
    if (editingId) {
      const idx = blocks.findIndex(b => b.id === editingId);
      if (idx > -1) blocks[idx] = { ...blocks[idx], ...payload };
    } else {
      blocks.push({ id: ++nextId, ...payload });
      activeDay = payload.day;
    }
    closeModal();
    render();
  });

  deleteBtn.addEventListener('click', () => {
    if (editingId) {
      blocks = blocks.filter(b => b.id !== editingId);
      closeModal();
      render();
    }
  });

  document.getElementById('newBlockBtn').addEventListener('click', () => openModal(null));
  document.getElementById('closeModalBtn').addEventListener('click', closeModal);
  document.getElementById('cancelBtn').addEventListener('click', closeModal);
  modalBackdrop.addEventListener('click', (e) => { if (e.target === modalBackdrop) closeModal(); });
  document.addEventListener('keydown', (e) => { if (e.key === 'Escape') closeModal(); });

  document.getElementById('filterBar').addEventListener('click', (e) => {
    const chip = e.target.closest('.chip');
    if (!chip) return;
    activeFilter = chip.dataset.filter;
    document.querySelectorAll('#filterBar .chip').forEach(c => c.classList.toggle('active', c === chip));
    render();
  });

  document.getElementById('autoProtectBtn').addEventListener('click', () => {
    let added = 0;
    DAYS.forEach(d => {
      const hasMorning = blocks.some(b => b.day === d.id && b.start < 9.5 && b.start + b.duration > 8);
      if (!hasMorning) {
        blocks.push({
          id: ++nextId,
          day: d.id,
          start: 8,
          duration: 1.5,
          title: 'Protected Deep Focus',
          cat: 'focus',
          note: 'Auto-shielded'
        });
        added++;
      }
    });
    if (added === 0) {
      const openAfternoon = DAYS.find(d => !blocks.some(b => b.day === d.id && b.start < 15 && b.start + b.duration > 13.5));
      if (openAfternoon) {
        blocks.push({
          id: ++nextId,
          day: openAfternoon.id,
          start: 13.5,
          duration: 1.5,
          title: 'Protected Deep Focus',
          cat: 'focus',
          note: 'Auto-shielded'
        });
      }
    }
    render();
  });

  render();
})();
</script>
</body>
</html>