/*
 * Mobile-first polish layer
 * Keeps the same URL, HTML, metadata and crawlable content on every device.
 * This file only changes presentation at narrow viewport widths.
 */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  overflow-wrap: anywhere;
}

button,
a,
input,
select,
textarea,
summary {
  -webkit-tap-highlight-color: transparent;
}

button,
.btn,
.icon-btn,
.preset-btn,
.nav-item,
.check-chip span,
summary {
  touch-action: manipulation;
}

.table-wrap {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-inline: contain;
}

/* Tablet / small laptop */
@media (max-width: 900px) {
  :root {
    --header: 64px;
  }

  .topbar {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }

  .sidebar {
    width: min(82vw, 320px);
    padding-left: max(12px, env(safe-area-inset-left));
    padding-bottom: max(18px, env(safe-area-inset-bottom));
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .main {
    width: 100%;
  }

  .hero {
    min-height: clamp(300px, 52vh, 390px);
  }

  .hero-content {
    width: 100%;
  }

  .content-section,
  .card,
  .hero {
    scroll-margin-top: calc(var(--header) + 12px);
  }

  .footer {
    padding-bottom: calc(36px + env(safe-area-inset-bottom));
  }
}

/* Phone */
@media (max-width: 640px) {
  body {
    font-size: 15px;
  }

  .topbar {
    gap: 8px;
  }

  .brand {
    min-width: 0;
    gap: 8px;
  }

  .brand > span:last-child {
    min-width: 0;
    max-width: 42vw;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .brand-mark {
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
  }

  .top-actions {
    gap: 5px;
  }

  .icon-btn {
    width: 44px;
    height: 44px;
    border-radius: 12px;
  }

  /* Fullscreen is less reliable/useful in mobile browsers; keep the feature in the drawer-free desktop UI. */
  .top-actions [data-fullscreen] {
    display: none;
  }

  .main {
    padding: 10px;
  }

  .section,
  .content-section {
    margin-top: 14px;
  }

  .hero {
    min-height: clamp(270px, 49svh, 340px);
    padding: 24px 12px;
    border-radius: 16px;
  }

  .hero::before {
    width: 360px;
    height: 360px;
    top: -210px;
    right: -170px;
  }

  .eyebrow {
    font-size: 11px;
    letter-spacing: .12em;
  }

  .hero-title {
    font-size: clamp(24px, 7.5vw, 32px);
    line-height: 1.2;
  }

  .hero-desc {
    margin-top: 10px;
    font-size: 14px;
    line-height: 1.65;
  }

  .live-clock,
  .tool-number {
    max-width: 100%;
    font-size: clamp(46px, 16vw, 76px);
    letter-spacing: -.065em;
    white-space: nowrap;
  }

  .live-date {
    margin-top: 10px;
    font-size: 16px;
  }

  .tool-display {
    padding: 18px 0;
  }

  .tool-caption {
    font-size: 13px;
  }

  .card,
  .content-section {
    padding: 16px;
    border-radius: 16px;
  }

  .card-title-row {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 14px;
  }

  .card-title-row > .button-row {
    width: 100%;
  }

  .card h2,
  .content-section h2 {
    font-size: 21px;
    line-height: 1.3;
  }

  .content-section h3 {
    font-size: 17px;
  }

  .grid {
    gap: 14px;
  }

  .form-grid {
    gap: 12px;
  }

  /* Avoid iOS focus zoom and keep form controls thumb-friendly. */
  .field input,
  .field select,
  .field textarea {
    min-height: 48px;
    font-size: 16px;
  }

  input[type="range"] {
    min-height: 44px;
  }

  input[type="checkbox"],
  input[type="radio"] {
    min-width: 20px;
    min-height: 20px;
  }

  .btn,
  .preset-btn {
    min-height: 48px;
  }

  .button-row:not(.compact) {
    width: 100%;
  }

  .button-row:not(.compact) > .btn {
    flex: 1 1 140px;
  }

  .stopwatch-controls .btn {
    min-width: 0;
  }

  .preset-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }

  .preset-btn {
    padding-inline: 8px;
    font-size: 14px;
  }

  .check-row {
    display: grid;
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 6px;
  }

  .check-chip span {
    width: 100%;
    min-width: 0;
    min-height: 44px;
    padding: 0;
  }

  .note {
    padding: 13px 14px;
    font-size: 13px;
  }

  .next-alarm {
    width: 100%;
    justify-content: center;
    margin-top: 18px;
    font-size: 13px;
  }

  .list-item {
    padding: 14px;
  }

  .list-actions {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
  }

  .list-actions .btn {
    width: 100%;
  }

  .timer-item-time {
    font-size: 30px;
  }

  .world-card {
    padding: 16px;
  }

  .world-time {
    font-size: clamp(32px, 11vw, 46px);
  }

  .world-meta {
    flex-wrap: wrap;
  }

  .meeting-row {
    padding: 12px;
  }

  .stat {
    padding: 15px;
  }

  .stat strong {
    font-size: 25px;
  }

  .date-result strong {
    font-size: clamp(38px, 13vw, 62px);
  }

  .table-wrap {
    margin-inline: -4px;
    max-width: calc(100% + 8px);
  }

  table {
    min-width: 560px;
  }

  th,
  td {
    padding: 11px 12px;
    white-space: nowrap;
  }

  .footer {
    margin-top: 20px;
    padding: 24px 16px calc(30px + env(safe-area-inset-bottom));
  }

  .footer-inner,
  .footer-links {
    gap: 10px 14px;
  }

  .toast-wrap {
    left: 10px;
    right: 10px;
    bottom: calc(10px + env(safe-area-inset-bottom));
  }

  .toast {
    min-width: 0;
    width: 100%;
    max-width: none;
  }

  .ring-overlay {
    padding: 12px max(12px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }

  .ring-card {
    padding: 30px 18px;
    border-radius: 22px;
  }

  .ring-card h2 {
    font-size: 26px;
  }

  dialog {
    width: min(100% - 18px, 520px);
    max-height: min(88svh, 760px);
    overflow: auto;
    border-radius: 18px;
  }

  .dialog-head,
  .dialog-body,
  .dialog-foot {
    padding-left: 16px;
    padding-right: 16px;
  }

  .dialog-foot .btn {
    flex: 1 1 0;
  }
}

/* Narrow phones */
@media (max-width: 390px) {
  .main {
    padding-inline: 8px;
  }

  .topbar {
    padding-inline: 8px;
  }

  .top-actions [data-open-settings] {
    display: none;
  }

  .brand > span:last-child {
    max-width: 38vw;
  }

  .hero {
    min-height: 260px;
  }

  .live-clock,
  .tool-number {
    font-size: clamp(43px, 15.5vw, 65px);
  }

  .preset-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .check-row {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}

/* Landscape phone: reduce vertical waste while keeping controls usable. */
@media (max-height: 520px) and (orientation: landscape) and (max-width: 900px) {
  .hero {
    min-height: 245px;
  }

  .live-clock,
  .tool-number {
    font-size: clamp(48px, 10vw, 74px);
  }
}

/* Accessibility: respect reduced motion. */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Share toolbar + source-code dialog on mobile */
@media (max-width: 900px) {
  .share-panel { padding: 0 18px; }
  .share-panel-inner { max-width: none; }
}

@media (max-width: 640px) {
  .share-panel { padding: 0 10px; }
  .share-panel-inner {
    margin-top: 4px;
    padding: 14px;
    align-items: flex-start;
    flex-direction: column;
    gap: 12px;
    border-radius: 14px;
  }
  .share-heading span { line-height: 1.45; }
  .share-actions {
    width: 100%;
    justify-content: flex-start;
    gap: 7px;
  }
  .share-btn {
    width: 40px;
    height: 40px;
    flex: 0 0 40px;
    border-radius: 7px;
  }
  .source-code-btn {
    min-height: 40px;
    padding-inline: 13px;
  }
  .source-dialog {
    width: min(100% - 14px, 640px);
    max-height: 92svh;
    border-radius: 14px;
  }
  .source-dialog .dialog-head { padding: 16px; }
  .source-dialog .dialog-body { padding: 16px; }
  .source-preview-wrap {
    justify-content: start;
    min-height: 264px;
    padding: 8px;
  }
  .source-preview-wrap iframe {
    width: 100% !important;
    min-width: 0;
  }
}

@media (max-width: 390px) {
  .share-actions { gap: 6px; }
  .share-btn { width: 38px; height: 38px; flex-basis: 38px; }
  .source-code-btn { width: 100%; margin-top: 2px; }
}

/* NowClock V3 mobile refinements */
@media (max-width:640px) {
  .world-hero-location { flex-wrap:wrap; gap:4px 7px; font-size:16px; }
  .world-hero .live-clock { margin-top:16px; }
  .ad-banner { margin-top:12px; }
  .ad-banner-inner { min-height:100px; border-radius:14px; }
  .ad-placeholder { min-height:98px; padding:12px; }
  .ad-placeholder span { max-width:270px; }
  .world-clock-panel .button-row.compact { display:grid; grid-template-columns:1fr 1fr; width:100%; }
  .world-clock-panel .button-row.compact .btn { width:100%; min-height:44px; }
  .world-info-grid { grid-template-columns:1fr; }
  .world-info-grid > div { padding:14px; }
  .world-add-dialog { width:min(100% - 18px,560px); max-height:88svh; }
  .site-footer { margin-top:20px; padding:24px 14px calc(28px + env(safe-area-inset-bottom)); }
  .footer-stack { gap:16px; }
  .footer-common { gap:10px 14px; line-height:1.5; }
  .footer-share { width:100%; display:grid; grid-template-columns:repeat(5,34px); justify-content:center; gap:8px; }
  .footer-share-label { grid-column:1/-1; }
  .footer-share .source-code-btn { grid-column:1/-1; width:100%; min-height:42px; margin-top:2px; }
  .footer-copy { line-height:1.6; }
}

@media (max-width:390px) {
  .footer-share { grid-template-columns:repeat(4,34px); }
  .world-clock-panel .button-row.compact { grid-template-columns:1fr; }
}

@media (max-width:640px){.brand small{display:block;font-size:9px;line-height:1.15;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:38vw}}

/* NowClock mobile footer share alignment patch v3.2
   Keep the 8 SNS icons in a horizontal row on mobile instead of stacking vertically.
   The source-code button moves to its own centered row below the icons. */
@media (max-width: 640px) {
  .site-footer .footer-share {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
  }

  .site-footer .footer-share-label {
    width: 100%;
    text-align: center;
  }

  .site-footer .footer-share .share-actions {
    width: 100%;
    max-width: 420px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: 6px;
  }

  .site-footer .footer-share .share-btn {
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    border-radius: 7px;
  }

  .site-footer .footer-share .source-code-btn {
    flex: 0 0 100%;
    width: min(160px, 100%);
    max-width: 160px;
    min-height: 40px;
    margin: 4px auto 0;
    writing-mode: horizontal-tb;
    white-space: nowrap;
  }
}

@media (max-width: 390px) {
  .site-footer .footer-share .share-actions {
    gap: 6px;
  }

  .site-footer .footer-share .share-btn {
    width: 34px;
    height: 34px;
    flex-basis: 34px;
  }
}
