/*
 * Shared shell for individual tools.
 *
 * This file owns the shared page frame, header controls, transient feedback
 * and small-screen safety rules. Tool-specific content keeps its local styles.
 */

html[data-tool-layout] {
  --tool-shell-max: 40rem;
  --tool-shell-gutter: clamp(0.75rem, 2vw, 1.5rem);
  --tool-type-title: 1.125rem;
  --tool-type-body: 0.875rem;
  --tool-type-control: 0.875rem;
  --tool-type-support: 0.8125rem;
  --tool-type-meta: 0.75rem;
}

html[data-tool-layout="mini"] { --tool-shell-max: 22.5rem; }
html[data-tool-layout="compact"] { --tool-shell-max: 32.5rem; }
html[data-tool-layout="standard"] { --tool-shell-max: 40rem; }
html[data-tool-layout="wide"] { --tool-shell-max: 47.5rem; }

html[data-tool-layout] body {
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  padding: var(--tool-shell-gutter);
}

html[data-tool-layout] :where(.app, .calc-app) {
  width: min(100%, var(--tool-shell-max));
  max-width: var(--tool-shell-max);
  flex: 0 0 auto;
  border-radius: 1rem;
}

html[data-tool-layout] :where(.header, .calc-header).tool-shell-header {
  display: grid;
  grid-template-columns: minmax(2.75rem, auto) minmax(0, 1fr) minmax(2.75rem, auto);
  align-items: center;
  gap: 0.5rem;
  min-height: 3.5rem;
  padding: 0.5rem 0.75rem;
}

html[data-tool-layout] .tool-shell-header > h1 {
  position: static;
  left: auto;
  width: auto;
  min-width: 0;
  margin: 0;
  transform: none;
  order: initial;
  text-align: center;
  font-size: var(--tool-type-title);
  line-height: 1.35;
  overflow-wrap: anywhere;
  pointer-events: auto;
}

/* Readable type floors shared by legacy and current tools. */
html[data-tool-layout] :where(label, legend, summary) {
  font-size: var(--tool-type-body) !important;
  line-height: 1.45;
}

html[data-tool-layout] :where(
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="hidden"]):not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="color"]),
  select,
  textarea
) {
  font-size: 1rem !important;
}

html[data-tool-layout] :where(
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="hidden"]):not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="color"]),
  textarea
)::placeholder {
  font-size: 1em !important;
}

html[data-tool-layout] .field {
  min-width: 0;
}

html[data-tool-layout] .timer-inputs input {
  font-size: 1.25rem !important;
}

html[data-tool-layout] #petAge {
  font-size: 1.5rem !important;
}

html[data-tool-layout] :where(
  .action-btn,
  .bcs-btn,
  .breed-item,
  .btn,
  .btn-copy-small,
  .emoji-tab,
  .fuel-tab,
  .gender-btn,
  .journey-tab,
  .main-tab,
  .min-wage button,
  .mode-tab,
  .num-btn,
  .preset,
  .preset-btn,
  .q-btn,
  .quick-add,
  .quick-btn,
  .related-chip,
  .scale-btn,
  .size-tab,
  .speed-preset,
  .tab,
  .tool-card,
  .toggle-opt,
  .type-tab,
  .unit-tab
) {
  font-size: var(--tool-type-control) !important;
  line-height: 1.35;
}

html[data-tool-layout] :where(
  .act-btn,
  .add-btn,
  .copy-btn,
  .btn-now,
  .btn-swap,
  .btn-today,
  .example-chip,
  .history-item,
  .mission-choice,
  .paste-btn,
  .recent-label,
  .reset-btn,
  .reset-opt,
  .schedule-toggle,
  .storage-clear,
  .sub-header,
  .support-info,
  .tab-group-label,
  .tap-reset,
  .toggle-label,
  .tool-card .title,
  .tz-label
) {
  font-size: var(--tool-type-control) !important;
  line-height: 1.45;
}

html[data-tool-layout] .mission-choice strong {
  font-size: var(--tool-type-control) !important;
}

html[data-tool-layout] :where(.tag, .tool-tag, .related-tag) {
  font-size: var(--tool-type-support) !important;
  line-height: 1.4;
}

html[data-tool-layout] .tool-name {
  font-size: var(--tool-type-body) !important;
  line-height: 1.4;
}

html[data-tool-layout] :where(
  .section-title,
  .date-section-title,
  .guide-title,
  .report-title,
  .scale-label,
  .related-tools > h2,
  .related-tools > h3
) {
  font-size: var(--tool-type-body) !important;
  line-height: 1.45;
}

html[data-tool-layout] :where(
  .guide-content,
  .report-box,
  .work-context
) {
  font-size: var(--tool-type-body) !important;
  line-height: 1.65;
}

html[data-tool-layout] .guide-content table {
  font-size: var(--tool-type-meta) !important;
  line-height: 1.5;
}

html[data-tool-layout] :where(
  .guide-content,
  .report-box,
  .work-context
) :where(p, li, dd, dt) {
  font-size: var(--tool-type-body) !important;
  line-height: 1.65;
}

html[data-tool-layout] :where(
  small,
  .bottom-bar .info,
  .chip-hex,
  .chip-label,
  .char-count,
  .converted-info,
  .drop-zone .sub,
  .dropzone-sub,
  .field-label .optional,
  .field-label .required,
  .gauge-labels span,
  .history-item .idx,
  .invest-note,
  .ip-type-badge,
  .limit-note,
  .meal-card .meal-unit,
  .price-date,
  .price-source,
  .price-warning,
  .privacy,
  .progress-text,
  .result-sub,
  .section-title .badge,
  .setting-note,
  .size-tab span,
  .stage-bar .seg,
  .status-badge,
  .storage-note,
  #curTotal,
  #numberOptionHelp
) {
  font-size: var(--tool-type-meta) !important;
  line-height: 1.5;
}

html[data-tool-layout] :where(
  .info-item .label,
  .meal-card .meal-label
) {
  font-size: var(--tool-type-body) !important;
  line-height: 1.45;
}

html[data-tool-layout] .info-item .value[style*="font-size:11px"] {
  font-size: var(--tool-type-support) !important;
  line-height: 1.45;
}

html[data-tool-layout] .result-sub {
  font-size: var(--tool-type-body) !important;
}

html[data-tool-layout] :where(.r-sub, .s-sub, .stat-sub) {
  font-size: var(--tool-type-support) !important;
  line-height: 1.5;
}

html[data-tool-layout] :where(
  .r-label,
  .s-label,
  .stat-label
) {
  font-size: 0.8125rem !important;
  line-height: 1.45;
}

html[data-tool-layout] .result-label {
  font-size: var(--tool-type-body) !important;
  line-height: 1.45;
}

html[data-tool-layout] :where(.footer, .site-policy-links) {
  font-size: var(--tool-type-meta) !important;
  line-height: 1.5;
}

html[data-tool-layout] .tool-shell-header > .tool-shell-back {
  justify-self: start;
  min-width: 2.75rem;
  min-height: 2.75rem;
  justify-content: center;
  padding: 0.4rem 0.65rem;
  white-space: nowrap;
  font-size: var(--tool-type-control);
}

html[data-tool-layout] .tool-header-actions {
  justify-self: end;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.375rem;
  min-width: 2.75rem;
}

html[data-tool-layout] .tool-header-actions > :where(a, button) {
  min-height: 2.75rem;
}

html[data-tool-layout] .tool-header-actions > a {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.4rem 0.65rem;
  white-space: nowrap;
  font-size: var(--tool-type-control);
}

html[data-tool-layout] .theme-toggle-tool.theme-toggle-inline {
  position: static;
  inset: auto;
  flex: 0 0 2.75rem;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 0.65rem;
  border-color: rgba(255, 255, 255, 0.28);
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  box-shadow: none;
}

html[data-tool-layout] .theme-toggle-tool.theme-toggle-inline:hover {
  background: rgba(255, 255, 255, 0.24);
  transform: none;
  box-shadow: none;
}

html[data-tool-layout] .tool-header-actions--search {
  width: min(17rem, 38vw);
}

html[data-tool-layout] .tool-header-actions--search .search-box {
  flex: 1 1 auto;
  min-width: 0;
}

html[data-tool-layout] .tool-header-actions--search .search-box input {
  width: 100%;
  min-width: 0;
}

html[data-tool-layout] .tool-confirm-dialog {
  width: min(28rem, calc(100vw - 1.5rem));
  max-height: calc(100dvh - 1.5rem);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 1rem;
  background: var(--bg-app);
  color: var(--text-primary);
  box-shadow: 0 1.5rem 5rem rgba(0, 0, 0, 0.35);
}

html[data-tool-layout] .tool-confirm-dialog::backdrop {
  background: rgba(15, 23, 42, 0.58);
  backdrop-filter: blur(0.2rem);
}

html[data-tool-layout] .tool-confirm-dialog__body {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.25rem;
}

html[data-tool-layout] .tool-confirm-dialog h2 {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.4;
}

html[data-tool-layout] .tool-confirm-dialog p {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.875rem;
  line-height: 1.65;
}

html[data-tool-layout] .tool-confirm-dialog__actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem;
  margin-top: 0.25rem;
}

html[data-tool-layout] .tool-confirm-dialog__actions button {
  min-height: 2.75rem;
  border: 0;
  border-radius: 0.625rem;
  background: var(--bg-btn);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
}

html[data-tool-layout] .tool-confirm-dialog__actions .tool-confirm-dialog__confirm {
  background: var(--accent);
  color: var(--text-on-accent);
}

html[data-tool-layout] .toast {
  left: 50%;
  bottom: max(0.75rem, env(safe-area-inset-bottom));
  width: max-content;
  max-width: min(30rem, calc(100vw - 1.5rem));
  padding: 0.75rem 1rem;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 0.75rem;
  background: var(--accent);
  color: var(--text-on-accent);
  box-shadow: var(--shadow-lg);
  font-size: var(--tool-type-body);
  font-weight: 600;
  line-height: 1.45;
  text-align: center;
  white-space: normal;
  overflow-wrap: anywhere;
  pointer-events: none;
  z-index: 1100;
  opacity: 0;
  transform: translate(-50%, calc(100% + 2rem));
  transition: transform 0.2s ease, opacity 0.2s ease;
}

html[data-tool-layout] .toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

html[data-tool-layout] :where(.guide-content, .work-context, .report-box) {
  overflow-wrap: anywhere;
}

html[data-tool-layout] .scroll-top-btn {
  position: fixed;
  right: max(1.5rem, env(safe-area-inset-right));
  bottom: max(1.5rem, env(safe-area-inset-bottom));
  z-index: 900;
  display: grid;
  place-items: center;
  width: 2.75rem;
  height: 2.75rem;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--text-on-accent);
  box-shadow: var(--shadow-accent);
  font: 700 1.25rem/1 system-ui, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s, transform 0.2s, background 0.2s;
}

html[data-tool-layout] .scroll-top-btn.visible {
  opacity: 1;
  visibility: visible;
}

html[data-tool-layout] .scroll-top-btn:hover {
  background: var(--accent-hover);
  transform: scale(1.06);
}

@media (max-width: 900px) {
  html[data-tool-layout] .scroll-top-btn {
    display: none;
  }
}

@media (max-width: 640px) {
  html[data-tool-layout] {
    --tool-shell-gutter: 0.75rem;
  }

  html[data-tool-layout] :where(.app, .calc-app) {
    border-radius: 0.875rem;
  }

  html[data-tool-layout] :where(.header, .calc-header).tool-shell-header {
    padding-inline: 0.625rem;
  }

  html[data-tool-layout] :where(
    input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="hidden"]),
    select,
    textarea
  ) {
    min-width: 0;
    max-width: 100%;
    min-height: 2.75rem;
  }

  html[data-tool-layout] input[type="range"] {
    min-width: 0;
    max-width: 100%;
  }

  html[data-tool-layout] button:not(.scroll-top-btn) {
    min-height: 2.75rem;
    touch-action: manipulation;
  }

  html[data-tool-layout] :is(
    .char-btn-small,
    .del-btn,
    .sub-del,
    .pw-btn,
    .include-help-toggle,
    .quick-btn,
    .mode-tab,
    .toggle-opt,
    .btn-copy-small
  ) {
    min-width: 2.75rem;
  }

  html[data-tool-layout] .char-grid,
  html[data-tool-layout] .emoji-grid,
  html[data-tool-layout] .num-grid {
    grid-template-columns: repeat(auto-fill, minmax(2.75rem, 1fr));
  }

  html[data-tool-layout] .bcs-selector {
    grid-template-columns: repeat(auto-fit, minmax(2.75rem, 1fr));
  }

  html[data-tool-layout] .emoji-btn {
    width: 2.75rem;
    height: 2.75rem;
  }

  html[data-tool-layout] .bottom-bar {
    gap: 0.5rem;
    flex-wrap: wrap;
  }

  html[data-tool-layout] .bottom-bar > .info {
    flex: 1 1 100%;
    min-width: 0;
  }

  html[data-tool-layout] .bottom-bar > :where(.bottom-actions, .info + div) {
    flex: 1 1 100%;
    min-width: 0;
  }

  html[data-tool-layout] .bottom-bar > .action-btn,
  html[data-tool-layout] .bottom-bar > :where(.bottom-actions, .info + div) > .action-btn {
    flex: 1 1 0;
    min-width: 0;
    min-height: 2.75rem;
  }

  html[data-tool-layout] .site-policy-links a {
    min-height: 2.75rem;
    display: inline-flex;
    align-items: center;
  }

  html[data-tool-layout] .tool-header-actions--search {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: stretch;
    width: 100%;
  }

  html[data-tool-layout] .tool-header-actions--search .search-box {
    width: auto;
  }
}

@media (max-width: 420px) {
  html[data-tool-layout] .tool-shell-header > .tool-shell-back,
  html[data-tool-layout] .tool-header-actions > a {
    width: 2.75rem;
    padding-inline: 0;
    overflow: hidden;
    color: transparent;
    font-size: 0;
    gap: 0;
  }

  html[data-tool-layout] .tool-shell-header > .tool-shell-back::after,
  html[data-tool-layout] .tool-header-actions > a::after {
    content: attr(data-tool-short);
    color: #fff;
    font-size: 1rem;
    line-height: 1;
  }
}
