.template-gallery {
  --template-ink: var(--drive-text, #202939);
  --template-muted: var(--drive-muted, #546174);
  --template-line: var(--drive-line, #dce4ef);
  --template-surface: var(--drive-surface, #fff);
  --template-accent: var(--drive-accent, #2457c5);
  --template-selected: var(--drive-selected, #eaf1ff);
  --template-stage: #eaf0f8;
  color: var(--template-ink);
  min-width: 0;
}
:root[data-office-theme="dark"] .template-gallery {
  --template-ink: var(--drive-text, #edf3fb);
  --template-muted: var(--drive-muted, #b7c5d8);
  --template-line: var(--drive-line, #41516a);
  --template-surface: var(--drive-surface, #222b38);
  --template-accent: var(--drive-accent, #a3befc);
  --template-selected: var(--drive-selected, #33486b);
  --template-stage: #2c394c;
}
.template-toolbar { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 9px; min-width: 0; }
.template-heading { margin: 0; color: var(--template-ink); font-size: clamp(1.25rem, 2.2vw, 1.9rem); font-weight: 690; letter-spacing: -.02em; }
.template-search {
  display: flex;
  align-items: center;
  flex: 1 1 280px;
  max-width: 500px;
  min-width: 0;
  gap: 9px;
  padding-inline: 12px;
  border: 1px solid var(--template-line);
  border-radius: 8px;
  background: var(--template-surface);
  color: var(--template-muted);
}
.template-search:focus-within { border-color: var(--template-accent); outline: 1px solid var(--template-accent); }
.template-search input { width: 100%; min-width: 0; height: 41px; padding: 0; border: 0; outline: 0; background: transparent; color: var(--template-ink); font-size: .9rem; }
.template-search input::placeholder { color: var(--template-muted); opacity: 1; }
.template-search-icon { position: relative; flex: none; width: 13px; height: 13px; border: 1.5px solid; border-radius: 50%; margin-inline-end: 3px; }
.template-search-icon::after { content: ""; position: absolute; width: 6px; height: 1.5px; background: currentColor; right: -4px; bottom: -2px; transform: rotate(45deg); }
.template-categories { display: flex; gap: 7px; flex-wrap: wrap; align-items: center; margin-block: 14px 6px; }
.template-filter, .template-clear {
  min-height: 35px;
  border: 1px solid var(--template-line);
  border-radius: 7px;
  padding: 5px 10px;
  background: var(--template-surface);
  color: var(--template-ink);
  font-size: .83rem;
  font-weight: 565;
}
.template-filter:hover, .template-clear:hover { border-color: var(--template-accent); color: var(--template-accent); }
.template-filter[aria-pressed="true"] { border-color: var(--template-accent); background: var(--template-selected); color: var(--template-accent); }
.template-result-count { margin: 9px 0; color: var(--template-muted); font-size: .8rem; min-height: 1.2em; }
.template-grid { display: grid; grid-template-columns: repeat(auto-fill,minmax(min(177px,100%),1fr)); align-items: stretch; gap: 11px; }
.template-item { min-width: 0; }
.template-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
  min-width: 0;
  height: 100%;
  padding: 0;
  border: 1px solid var(--template-line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--template-surface);
  text-align: start;
  color: var(--template-ink);
}
.template-card:not(:disabled):hover { border-color: var(--template-accent); box-shadow: 0 2px 5px #19366613; }
.template-card:focus-visible, .template-filter:focus-visible, .template-clear:focus-visible { outline: 2px solid var(--template-accent); outline-offset: 2px; }
.template-card:disabled { cursor: wait; opacity: .72; }
.template-preview {
  position: relative;
  display: grid;
  place-items: center;
  flex: none;
  width: 100%;
  height: clamp(143px, 13vw, 174px);
  min-width: 0;
  overflow: hidden;
  border-block-end: 1px solid var(--template-line);
  background: var(--template-stage);
  pointer-events: none;
  user-select: none;
}
.template-paper-fallback { display: grid; place-items: center; width: auto; height: calc(100% - 22px); aspect-ratio: 210/297; background: #fff; border: 1px solid #cdd8e6; border-radius: 2px; }
.template-blank-plus { display: grid; place-items: center; width: 34px; height: 34px; border: 1px solid #becff4; border-radius: 50%; color: #2457c5; background: #edf3ff; font-size: 1.4rem; font-weight: 620; line-height: 1; }
.template-card[data-template-id="blank"] .template-preview[data-preview-ready="true"]::after { content: "+"; position: absolute; inset: 50% auto auto 50%; transform: translate(-50%, -50%); display: grid; place-items: center; width: 34px; height: 34px; border: 1px solid #becff4; border-radius: 50%; color: #2457c5; background: #edf3ff; font-size: 1.4rem; font-weight: 620; line-height: 1; }
.template-card-info { display: grid; align-content: start; gap: 4px; padding: 9px 9px 10px; }
.template-card-title { font-size: .87rem; font-weight: 690; line-height: 1.25; overflow-wrap: anywhere; }
.template-card-description { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; color: var(--template-muted); font-size: .77rem; line-height: 1.3; overflow-wrap: anywhere; }
.template-empty { display: grid; justify-items: center; gap: 7px; padding: 23px 12px; border: 1px solid var(--template-line); border-radius: 8px; background: var(--template-surface); text-align: center; }
.template-empty p { margin: 0; color: var(--template-muted); font-size: .875rem; }
.template-create-error { padding: 8px 10px; margin: 6px 0; border: 1px solid var(--template-line); border-radius: 6px; color: var(--drive-danger, #ad3049); background: var(--template-surface); font-size: .85rem; }

@media (max-width: 650px) {
  .template-toolbar { gap: 7px; }
  .template-search { flex-basis: 100%; max-width: none; }
  .template-categories { gap: 5px; margin-block-start: 10px; }
  .template-filter { font-size: .79rem; min-height: 32px; padding-inline: 8px; }
  .template-grid { grid-template-columns: repeat(auto-fill,minmax(min(144px,100%),1fr)); gap: 8px; }
  .template-preview { height: 138px; }
}
