<script setup lang="ts">
interface List {
  id: string;
  name: string;
}
interface Task {
  id: string;
  listId: string;
  title: string;
  description: string | null;
  source: string;
  consumed: boolean;
  createdAt: string;
}

const { auth, api } = useAuth();

const lists = ref<List[]>([]);
const selectedId = ref<string | null>(null);
const tasks = ref<Task[]>([]);
const loadingLists = ref(true);
const loadingTasks = ref(false);
const error = ref<string | null>(null);

const title = ref("");
const description = ref("");
const showNote = ref(false);
const adding = ref(false);
const titleInput = ref<HTMLInputElement | null>(null);

const sheetOpen = ref(false);
const expandedId = ref<string | null>(null);

const selectedList = computed(() => lists.value.find((l) => l.id === selectedId.value) ?? null);

const today = new Intl.DateTimeFormat(undefined, {
  weekday: "long",
  month: "long",
  day: "numeric",
}).format(new Date());

async function refreshLists() {
  loadingLists.value = true;
  error.value = null;
  try {
    lists.value = await api<List[]>("/lists");
    if (lists.value.length && !lists.value.some((l) => l.id === selectedId.value)) {
      await selectList(lists.value[0]!.id);
    }
  } catch (e) {
    error.value = (e as Error).message;
  } finally {
    loadingLists.value = false;
  }
}

async function selectList(id: string) {
  selectedId.value = id;
  sheetOpen.value = false;
  expandedId.value = null;
  await refreshTasks();
}

async function refreshTasks() {
  if (!selectedId.value) return;
  loadingTasks.value = true;
  error.value = null;
  try {
    tasks.value = await api<Task[]>(`/lists/${selectedId.value}/tasks`);
  } catch (e) {
    error.value = (e as Error).message;
  } finally {
    loadingTasks.value = false;
  }
}

async function addTask() {
  const t = title.value.trim();
  if (!t || !selectedId.value || adding.value) return;
  adding.value = true;
  error.value = null;
  try {
    const created = await api<Task>("/tasks", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({
        title: t,
        description: description.value.trim() || undefined,
        listId: selectedId.value,
      }),
    });
    tasks.value = [...tasks.value, created];
    title.value = "";
    description.value = "";
    showNote.value = false;
    titleInput.value?.focus();
  } catch (e) {
    error.value = (e as Error).message;
  } finally {
    adding.value = false;
  }
}

function toggleNote() {
  showNote.value = !showNote.value;
  if (!showNote.value) description.value = "";
}

function toggleTask(t: Task) {
  expandedId.value = expandedId.value === t.id ? null : t.id;
}

onMounted(() => {
  // The auth plugin gates before mount, so this is normally authenticated.
  // Safety net: if not, drive login instead of calling the API (no 401 banner).
  if (auth.isAuthenticated) refreshLists();
  else auth.login();
});
</script>

<template>
  <div class="app">
    <header class="masthead">
      <div class="masthead-row">
        <p class="brand">ClaudeDo</p>
        <div class="who" v-if="auth.user">
          <span class="email">{{ auth.user.email || auth.user.name }}</span>
          <button class="link" @click="auth.logout()">Sign out</button>
        </div>
      </div>
      <h1>{{ selectedList?.name ?? "Inbox" }}</h1>
      <p class="date">{{ today }}<template v-if="tasks.length"> · {{ tasks.length }} {{ tasks.length === 1 ? "task" : "tasks" }}</template></p>
    </header>

    <main class="content">
      <p v-if="error" class="error" role="alert">{{ error }}</p>

      <template v-if="loadingLists">
        <p class="muted">Loading lists…</p>
      </template>

      <template v-else-if="!lists.length">
        <div class="empty">
          <p class="empty-mark">◍</p>
          <p class="empty-title">No lists yet</p>
          <p class="muted">Sync your lists from the ClaudeDo desktop app, then add tasks here.</p>
        </div>
      </template>

      <template v-else>
        <section class="tasks" aria-label="Tasks">
          <p v-if="loadingTasks" class="muted">Loading…</p>
          <div v-else-if="!tasks.length" class="empty">
            <p class="empty-mark">✓</p>
            <p class="empty-title">All clear</p>
            <p class="muted">Nothing in {{ selectedList?.name }} yet. Capture something below.</p>
          </div>
          <ul v-else class="task-list">
            <li v-for="(t, i) in tasks" :key="t.id" :style="{ '--i': i }">
              <button
                class="task"
                :class="{ open: expandedId === t.id }"
                :aria-expanded="expandedId === t.id"
                @click="toggleTask(t)"
              >
                <p class="task-title">{{ t.title }}</p>
                <p v-if="t.description" class="task-desc">{{ t.description }}</p>
              </button>
            </li>
          </ul>
        </section>
      </template>
    </main>

    <footer v-if="lists.length" class="dock">
      <button
        v-if="lists.length > 1"
        class="list-pill"
        type="button"
        :aria-expanded="sheetOpen"
        @click="sheetOpen = !sheetOpen"
      >
        <span class="pill-label">List</span>
        <span class="pill-name">{{ selectedList?.name }}</span>
        <span class="pill-chevron" :class="{ up: sheetOpen }" aria-hidden="true">▾</span>
      </button>

      <form class="composer" @submit.prevent="addTask">
        <input
          v-if="showNote"
          v-model="description"
          class="input note"
          type="text"
          placeholder="Add a note…"
          autocomplete="off"
          enterkeyhint="done"
          aria-label="Task description"
        />
        <div class="capture">
          <button
            class="note-toggle"
            type="button"
            :class="{ on: showNote }"
            :aria-pressed="showNote"
            aria-label="Toggle note field"
            @click="toggleNote"
          >
            ✎
          </button>
          <input
            ref="titleInput"
            v-model="title"
            class="input"
            type="text"
            :placeholder="`Add to ${selectedList?.name ?? 'list'}…`"
            autocomplete="off"
            enterkeyhint="done"
            aria-label="Task title"
          />
          <button class="add" type="submit" :disabled="!title.trim() || adding" aria-label="Add task">
            <span v-if="adding" class="spin" aria-hidden="true"></span>
            <span v-else aria-hidden="true">↑</span>
          </button>
        </div>
      </form>
    </footer>

    <Transition name="fade">
      <div v-if="sheetOpen" class="backdrop" @click="sheetOpen = false" />
    </Transition>
    <Transition name="slide">
      <nav v-if="sheetOpen" class="sheet" aria-label="Switch list">
        <div class="sheet-handle" aria-hidden="true"></div>
        <p class="sheet-title">Lists</p>
        <button
          v-for="l in lists"
          :key="l.id"
          class="sheet-item"
          :class="{ active: l.id === selectedId }"
          @click="selectList(l.id)"
        >
          <span class="sheet-name">{{ l.name }}</span>
          <span v-if="l.id === selectedId" class="sheet-check" aria-hidden="true">✓</span>
        </button>
      </nav>
    </Transition>
  </div>
</template>

<style scoped>
.app {
  --bg: #f4f1ea;
  --card: #fffdf8;
  --border: #e3ddd0;
  --text: #211c15;
  --muted: #837a6b;
  --accent: #c75b39;
  --accent-soft: #f3e3dc;
  --accent-contrast: #fffdf8;
  --danger: #b42318;
  --shadow: 0 1px 2px rgb(33 28 21 / 0.05), 0 4px 16px rgb(33 28 21 / 0.04);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: "Spline Sans", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* ——— Masthead: compact, title = current list ——— */
.masthead {
  padding: max(0.85rem, env(safe-area-inset-top)) 1.25rem 0.6rem;
}
.masthead-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
}
.brand {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
}
.masthead h1 {
  margin: 0.35rem 0 0;
  font-family: "Fraunces", Georgia, serif;
  font-weight: 600;
  font-size: clamp(1.6rem, 7vw, 2.1rem);
  line-height: 1.05;
  letter-spacing: -0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.date {
  margin: 0.25rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}
.who {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  min-width: 0;
}
.email {
  display: none;
  font-size: 0.78rem;
  color: var(--muted);
  max-width: 12rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.link {
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.5rem 0;
  cursor: pointer;
  touch-action: manipulation;
}
.link:active {
  color: var(--accent);
}

.content {
  flex: 1;
  padding: 0.25rem 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  overflow-y: auto;
  overscroll-behavior-y: contain;
}

/* ——— Tasks: dense divided rows, tap to expand ——— */
.task-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.task-list li {
  animation: rise 0.3s cubic-bezier(0.2, 0.7, 0.3, 1) both;
  animation-delay: calc(var(--i, 0) * 25ms);
}
.task-list li + li {
  border-top: 1px solid var(--border);
}
@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
.task {
  display: block;
  width: 100%;
  min-height: 48px;
  padding: 0.65rem 0.95rem;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
}
.task:active {
  background: color-mix(in srgb, var(--accent-soft) 45%, transparent);
}
.task-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 500;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.task-desc {
  margin: 0.2rem 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
  white-space: pre-wrap;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}
.task.open .task-title,
.task.open .task-desc {
  -webkit-line-clamp: unset;
  display: block;
}
.task.open {
  background: color-mix(in srgb, var(--accent-soft) 30%, transparent);
}

.empty {
  margin-top: 12vh;
  text-align: center;
  padding: 0 1.5rem;
}
.empty-mark {
  margin: 0 0 0.5rem;
  font-size: 2rem;
  color: var(--accent);
}
.empty-title {
  font-family: "Fraunces", Georgia, serif;
  font-size: 1.35rem;
  font-weight: 600;
  margin: 0 0 0.35rem;
}
.muted {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
  margin: 0;
}
.error {
  background: #fef3f2;
  color: var(--danger);
  border: 1px solid #fecdca;
  border-radius: 12px;
  padding: 0.65rem 0.85rem;
  font-size: 0.875rem;
  margin: 0;
}

/* ——— Dock: list switcher + capture, all in the thumb zone ——— */
.dock {
  position: sticky;
  bottom: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.6rem 1rem calc(0.6rem + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.list-pill {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  align-self: flex-start;
  max-width: 100%;
  min-height: 40px;
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: var(--shadow);
}
.pill-label {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}
.pill-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pill-chevron {
  color: var(--accent);
  transition: transform 0.2s;
}
.pill-chevron.up {
  transform: rotate(180deg);
}

.composer {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
.capture {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.input {
  flex: 1;
  width: 100%;
  min-width: 0;
  min-height: 50px;
  padding: 0.7rem 1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  /* 16px floor prevents iOS focus zoom */
  font-size: 1rem;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow);
}
.input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: transparent;
}
.input.note {
  min-height: 44px;
  font-size: 1rem;
  animation: rise 0.2s ease both;
}
.note-toggle {
  flex: 0 0 auto;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, color 0.15s;
}
.note-toggle.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.add {
  flex: 0 0 auto;
  width: 50px;
  height: 50px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 1.3rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  display: grid;
  place-items: center;
  transition: transform 0.1s, opacity 0.15s;
}
.add:active:not(:disabled) {
  transform: scale(0.92);
}
.add:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.spin {
  width: 18px;
  height: 18px;
  border: 2px solid color-mix(in srgb, var(--accent-contrast) 35%, transparent);
  border-top-color: var(--accent-contrast);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ——— Bottom sheet: list picker in the thumb zone ——— */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgb(20 14 8 / 0.35);
}
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 21;
  max-width: 28rem;
  margin: 0 auto;
  max-height: 70dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--card);
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: 20px 20px 0 0;
  padding: 0.5rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 32px rgb(20 14 8 / 0.15);
}
.sheet-handle {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 0.25rem auto 0.5rem;
}
.sheet-title {
  margin: 0 0 0.25rem;
  padding: 0 0.5rem;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--muted);
}
.sheet-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  width: 100%;
  min-height: 52px;
  padding: 0.7rem 0.85rem;
  border: 0;
  border-radius: 12px;
  background: none;
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  touch-action: manipulation;
}
.sheet-item:active {
  background: var(--accent-soft);
}
.sheet-item.active {
  color: var(--accent);
  font-weight: 600;
}
.sheet-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sheet-check {
  color: var(--accent);
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
.slide-enter-active,
.slide-leave-active {
  transition: transform 0.25s cubic-bezier(0.2, 0.7, 0.3, 1);
}
.slide-enter-from,
.slide-leave-to {
  transform: translateY(100%);
}

/* ——— Larger screens: the enhancement, not the default ——— */
@media (min-width: 640px) {
  .masthead,
  .content,
  .dock {
    padding-left: max(1.25rem, calc(50vw - 19rem));
    padding-right: max(1.25rem, calc(50vw - 19rem));
  }
  .email {
    display: inline;
  }
  .sheet {
    border-radius: 20px;
    bottom: 1rem;
    border-bottom: 1px solid var(--border);
  }
}

@media (prefers-reduced-motion: reduce) {
  .task-list li,
  .input.note {
    animation: none;
  }
  .slide-enter-active,
  .slide-leave-active,
  .fade-enter-active,
  .fade-leave-active {
    transition: none;
  }
}

@media (prefers-color-scheme: dark) {
  .app {
    --bg: #181410;
    --card: #221d17;
    --border: #352d24;
    --text: #f0eae0;
    --muted: #9d9281;
    --accent: #e0795a;
    --accent-soft: #3a261f;
    --accent-contrast: #1d130f;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 4px 16px rgb(0 0 0 / 0.2);
  }
  .error {
    background: #2a1815;
    border-color: #5a2a22;
    color: #ff9a8d;
  }
  .backdrop {
    background: rgb(0 0 0 / 0.5);
  }
}
</style>
