@import url('https://fonts.googleapis.com/css2?family=Sofia+Sans:opsz,wght@8..144,400;8..144,600;8..144,700&display=swap');

:root {
  color-scheme: light;
  --page-bg: #f2efe8;
  --page-bg-2: #eae6dc;
  --ink: #171717;
  --muted: #6e6a62;
  --line: rgba(23, 23, 23, .13);
  --control-bg: rgba(255,255,255,.58);
  --control-active: #171717;
  --control-active-ink: #fff;
  font-family: "Sofia Sans", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--page-bg); color: var(--ink); }
body { min-height: 100vh; }
button, a { font: inherit; }

.home-shell {
  min-height: 100vh;
  padding: clamp(24px, 5vw, 64px);
  background: linear-gradient(180deg, var(--page-bg), var(--page-bg-2));
}
.home-header { max-width: 1180px; margin: 0 auto 28px; }
.home-header h1 { margin: 0; font-size: clamp(30px, 4vw, 54px); font-weight: 700; letter-spacing: -.03em; }
.home-header p { margin: 8px 0 0; color: var(--muted); font-size: 16px; }
.tool-grid { max-width: 1180px; margin: 0 auto; display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px; }
.tool-tile {
  display: flex; min-height: 180px; align-items: flex-end; padding: 24px;
  text-decoration: none; color: var(--ink); background: rgba(255,255,255,.52);
  border: 1px solid var(--line); border-radius: 20px;
  transition: transform .15s ease, background .15s ease, border-color .15s ease;
}
.tool-tile:hover { transform: translateY(-2px); background: rgba(255,255,255,.75); border-color: rgba(23,23,23,.24); }
.tool-tile:focus-visible { outline: 3px solid #000; outline-offset: 3px; }
.tool-title { font-size: 27px; font-weight: 700; letter-spacing: -.02em; }
.tool-subtitle { display: block; margin-top: 4px; color: var(--muted); font-size: 14px; font-weight: 500; }

.tracker-page { height: 100vh; overflow: hidden; padding: 8px; background: var(--page-bg); }
.tracker-shell { height: 100%; display: flex; flex-direction: column; gap: 7px; min-height: 0; }
.toolbar {
  flex: 0 0 auto; min-height: 44px; display: grid;
  grid-template-columns: auto minmax(0,1fr) auto auto; align-items: center; gap: 8px;
}
.back-button, .icon-button {
  width: 38px; height: 38px; border: 0; border-radius: 10px; background: var(--control-bg); color: var(--ink);
  display: inline-grid; place-items: center; text-decoration: none; cursor: pointer;
}
.back-button:hover, .icon-button:hover { background: rgba(255,255,255,.82); }
.back-button:focus-visible, .icon-button:focus-visible { outline: 3px solid #000; outline-offset: 2px; }
.back-button svg, .icon-button svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tracker-title { min-width: 0; font-size: clamp(19px, 2vw, 27px); font-weight: 700; letter-spacing: -.025em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.counter { height: 34px; padding: 0 10px; display: inline-flex; align-items: center; border-radius: 9px; background: rgba(255,255,255,.48); font-weight: 700; font-variant-numeric: tabular-nums; }
.filters { display: flex; gap: 4px; }
.filter-label { display: none; }
.icon-button.active { background: var(--control-active); color: var(--control-active-ink); }

.pencil-grid {
  flex: 1 1 auto; min-height: 0; width: 100%;
  display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); grid-template-rows: repeat(12, minmax(0, 1fr));
  gap: 3px;
}
.pencil-card {
  position: relative; min-width: 0; min-height: 0; overflow: hidden; border-radius: 5px;
  background: var(--card-color); color: var(--card-text); cursor: pointer; user-select: none;
  display: flex; flex-direction: column; justify-content: space-between;
  padding: clamp(2px, .33vh, 4px) clamp(3px, .38vw, 6px);
  border: 0; text-align: left;
  transition: filter .1s ease;
}
.pencil-card:hover { filter: saturate(1.04) brightness(1.015); }
.pencil-card:focus-visible, .pencil-section:focus-visible { outline: 2px solid #000; outline-offset: -2px; }
.pencil-card.owned, .pencil-section.owned { box-shadow: inset 0 0 0 3px #000; }
.pencil-card.filtered-out, .pencil-section.filtered-out { visibility: hidden; pointer-events: none; }
.pencil-number {
  font-size: clamp(7px, .88vh, 11px); line-height: 1; font-weight: 700; letter-spacing: .02em;
  font-variant-numeric: tabular-nums; opacity: .92;
}
.pencil-name {
  font-size: clamp(6.5px, .85vh, 11px); line-height: .98; font-weight: 700;
  overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  text-wrap: balance;
}
.checkmark {
  position: absolute; top: 3px; right: 4px; color: #000; font-size: clamp(11px, 1.35vh, 17px); line-height: 1;
  font-weight: 900; opacity: 0; transform: scale(.8); transition: opacity .1s ease, transform .1s ease;
}
.owned > .checkmark { opacity: 1; transform: scale(1); }

.double-card {
  position: relative; min-width: 0; min-height: 0; overflow: hidden; border-radius: 5px;
  display: grid; grid-template-rows: 1fr 1fr; background: transparent;
}
.pencil-section {
  position: relative; min-width: 0; min-height: 0; overflow: hidden; border: 0; margin: 0;
  background: var(--card-color); color: var(--card-text); cursor: pointer; user-select: none; text-align: left;
  padding: 1px 5px 1px 5px; display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 4px;
}
.pencil-section:first-child { border-radius: 5px 5px 0 0; }
.pencil-section:last-child { border-radius: 0 0 5px 5px; }
.pencil-section .pencil-number { font-size: clamp(6.5px, .77vh, 9px); }
.pencil-section .pencil-name { font-size: clamp(5.8px, .72vh, 8.5px); line-height: .95; -webkit-line-clamp: 1; }
.pencil-section .checkmark { top: 1px; right: 3px; font-size: clamp(9px, 1.05vh, 12px); }

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

@media (max-height: 760px) and (min-width: 900px) {
  .tracker-page { padding: 5px; }
  .tracker-shell { gap: 4px; }
  .toolbar { min-height: 36px; }
  .back-button, .icon-button { width: 32px; height: 32px; border-radius: 8px; }
  .counter { height: 30px; }
  .pencil-grid { gap: 2px; }
  .pencil-card { border-radius: 4px; padding: 2px 4px; }
  .pencil-card.owned, .pencil-section.owned { box-shadow: inset 0 0 0 2px #000; }
}

@media (max-width: 899px) {
  .tracker-page {
    min-height: 100vh; min-height: 100dvh; height: auto; overflow: visible;
    padding: 0 max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
  .tracker-shell { height: auto; gap: 12px; }
  .toolbar {
    position: sticky; top: 0; z-index: 20;
    grid-template-columns: 44px minmax(0, 1fr) auto;
    background: var(--page-bg); padding: max(8px, env(safe-area-inset-top)) 0 10px;
    border-bottom: 1px solid var(--line);
  }
  .tracker-page .back-button, .tracker-page .icon-button { min-height: 44px; height: 44px; }
  .tracker-page .back-button { width: 44px; }
  .tracker-title { font-size: 22px; }
  .counter { height: 36px; }
  .filters { grid-column: 1 / -1; gap: 6px; }
  .tracker-page .icon-button { flex: 1; width: auto; gap: 6px; display: flex; justify-content: center; }
  .filter-label { display: inline; font-size: 14px; font-weight: 600; }
  .pencil-grid {
    flex: none; grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: none; grid-auto-rows: minmax(100px, auto); gap: 7px;
  }
  .double-card { display: contents; }
  .pencil-card, .pencil-section {
    display: flex; flex-direction: column; align-items: stretch; justify-content: space-between;
    min-height: 100px; padding: 10px; gap: 14px; touch-action: manipulation;
  }
  .pencil-card, .pencil-section:first-child, .pencil-section:last-child { border-radius: 10px; }
  .pencil-number, .pencil-section .pencil-number { font-size: 14px; padding-right: 18px; }
  .pencil-name, .pencil-section .pencil-name {
    display: block; font-size: 13px; line-height: 1.2; overflow: visible;
    text-wrap: initial; overflow-wrap: anywhere;
  }
  .checkmark, .pencil-section .checkmark { top: 9px; right: 8px; font-size: 18px; color: var(--card-text); }
  .pencil-card.filtered-out, .pencil-section.filtered-out { display: none; }
}

@media (min-width: 540px) and (max-width: 899px) {
  .pencil-grid { grid-template-columns: repeat(5, minmax(0, 1fr)); }
}
