*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

html {
  scroll-padding-top: env(safe-area-inset-top, 0px);
}

body {
  margin: 0;
  background: var(--theme-bg);
  color: var(--theme-text);
  font-family:
    var(--font-ui-family, 'Hiragino Sans', 'Noto Sans JP', system-ui, sans-serif);
  -webkit-font-smoothing: antialiased;
  overflow: hidden; /* screens manage their own internal scroll */
}

img,
svg {
  max-width: 100%;
  display: block;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
}
button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

a {
  color: inherit;
}

/* --- App shell layout --- */
#app-root {
  position: relative;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

.mh-background-stage {
  position: fixed;
  inset: 0;
  z-index: var(--z-background);
  background: radial-gradient(circle at 20% -10%, color-mix(in srgb, var(--sky-tint, var(--color-accent)) 22%, transparent), transparent 60%),
    var(--theme-bg);
  transition: background var(--motion-duration-slow) var(--motion-ease-standard);
}

.mh-ambient-layer {
  position: fixed;
  inset: 0;
  z-index: var(--z-ambient);
  pointer-events: none;
}

.mh-screen-stack {
  position: absolute;
  inset: 0;
  z-index: var(--z-content);
}

.mh-screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 1;
  backface-visibility: hidden;
  transform-origin: 50% 50%;
  contain: paint;
}
.mh-screen[data-active='false'] {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}
.mh-screen[data-transition='enter'] {
  visibility: visible;
  pointer-events: auto;
  will-change: transform, opacity, filter;
}
.mh-screen[data-transition='exit'] {
  visibility: visible !important;
  pointer-events: none;
  will-change: transform, opacity;
}

.mh-screen-scroll {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: calc(var(--island-clearance) + var(--space-2)) var(--space-4) calc(96px + env(safe-area-inset-bottom, 0px));
}

/* --- Tab bar --- */
/* 幅は「画面幅 - 余白」を上限に、4タブが必ず1行で収まる最小幅を確保する。
   left:50% + translateX だと shrink-to-fit が右半分の幅に制限され折り返すため、inset で幅を決める。 */
.mh-tabbar {
  position: fixed;
  left: 50%;
  right: auto;
  width: min(440px, calc(100vw - 32px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)));
  margin: 0;
  transform: translateX(-50%);
  --tab-slot-width: calc((100% - 20px) / 4);
  bottom: calc(var(--space-3) + env(safe-area-inset-bottom, 0px));
  z-index: var(--z-island);
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--space-1);
  padding: var(--space-1);
  border-radius: var(--radius-pill);
  background: color-mix(in srgb, var(--theme-surface) 68%, transparent);
  backdrop-filter: var(--glass-backdrop-filter);
  -webkit-backdrop-filter: var(--glass-backdrop-filter);
  border: 1px solid var(--theme-border);
  box-shadow: var(--shadow-md);
  isolation: isolate;
  --tab-index: 0;
  transition: transform var(--motion-duration-normal) var(--motion-ease-standard), opacity var(--motion-duration-normal), box-shadow var(--motion-duration-fast);
  will-change: transform, opacity;
}
.mh-tabbar[data-mode='hidden'] {
  transform: translate3d(-50%, 160%, 0);
  opacity: 0;
  pointer-events: none;
}
.mh-tabbar[data-mode='compact'] {
  padding: 2px;
}

.mh-tab {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 0;
  padding: var(--space-2) var(--space-2);
  border-radius: var(--radius-pill);
  font-size: 11px;
  line-height: 1.2;
  white-space: nowrap;
  color: var(--theme-text-muted);
  background: transparent;
  transition: color var(--motion-duration-fast) var(--motion-ease-standard), transform var(--motion-duration-fast) var(--motion-ease-standard);
}
.mh-tab[aria-current='page'] {
  color: var(--theme-text);
}
.mh-tab:active {
  transform: scale(0.96);
}
.mh-tab-indicator {
  position: absolute;
  z-index: 1;
  left: var(--space-1);
  top: var(--space-1);
  bottom: var(--space-1);
  width: calc((100% - 28px) / 4);
  border-radius: var(--radius-pill);
  background: color-mix(in srgb, var(--color-accent) 19%, transparent);
  box-shadow: inset 0 1px 0 color-mix(in srgb, #fff 8%, transparent), 0 3px 14px color-mix(in srgb, var(--color-accent) 10%, transparent);
  width: var(--tab-slot-width);
  transform: translate3d(calc(var(--tab-index) * (var(--tab-slot-width) + 4px)), 0, 0);
  transition: transform var(--motion-duration-normal) var(--motion-spring), background var(--motion-duration-fast);
  will-change: transform;
  pointer-events: none;
}
.mh-tab:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}
.mh-tab-icon {
  width: 22px;
  height: 22px;
}

/* --- Dynamic Island --- */
.mh-island {
  position: fixed;
  top: calc(var(--space-3) + env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-island);
  width: min(48vw, 176px);
  min-width: 120px;
  max-width: min(92vw, 420px);
  height: 40px;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-pill);
  background: color-mix(in srgb, #000 70%, transparent);
  backdrop-filter: blur(20px) saturate(1.45);
  -webkit-backdrop-filter: blur(20px) saturate(1.45);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  font-size: 13px;
  font-weight: 650;
  line-height: 1.2;
  white-space: nowrap;
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.24), inset 0 1px 0 rgb(255 255 255 / 0.08);
  overflow: hidden;
  isolation: isolate;
  transition:
    width var(--motion-duration-normal) var(--motion-ease-emphasized),
    height var(--motion-duration-normal) var(--motion-ease-emphasized),
    padding var(--motion-duration-normal) var(--motion-ease-emphasized),
    border-radius var(--motion-duration-normal) var(--motion-ease-emphasized),
    background var(--motion-duration-normal) var(--motion-ease-standard),
    box-shadow var(--motion-duration-normal) var(--motion-ease-standard);
}
.mh-island::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(120deg, transparent 25%, rgb(255 255 255 / 0.08) 48%, transparent 65%);
  transform: translateX(-110%);
  opacity: 0;
  pointer-events: none;
}
.mh-island[data-state='expanded'],
.mh-island[data-state='reader'],
.mh-island[data-state='media'] {
  width: min(86vw, 360px);
  height: 72px;
  border-radius: var(--radius-xl);
  padding: var(--space-4);
  justify-content: flex-start;
}
.mh-island[data-state='search'] {
  width: min(78vw, 320px);
}
.mh-island[data-state='navigation'],
.mh-island[data-state='reader'] {
  box-shadow: 0 14px 36px rgb(0 0 0 / 0.3), inset 0 1px 0 rgb(255 255 255 / 0.1);
}
.mh-island[data-state='sync']::before,
.mh-island[data-state='download']::before {
  animation: mh-island-shimmer 1.35s var(--motion-ease-standard) both;
}
.mh-island[data-progress='true']::after {
  content: '';
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 4px;
  height: 2px;
  border-radius: 999px;
  background: linear-gradient(to right, var(--color-accent) var(--island-progress, 0%), transparent 0);
  opacity: .9;
}
.mh-island[data-tone='warning'] {
  background: color-mix(in srgb, var(--color-danger) 55%, #000 45%);
}
.mh-island[data-tone='success'] {
  background: color-mix(in srgb, var(--color-success) 45%, #000 55%);
}

/* --- Skeleton --- */
.mh-skeleton {
  position: relative;
  overflow: hidden;
  background: color-mix(in srgb, var(--theme-surface-elevated) 80%, transparent);
  border-radius: var(--radius-md);
}
.mh-skeleton::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--theme-text) 8%, transparent), transparent);
  animation: mh-shimmer 1.6s ease-in-out infinite;
}
[data-motion-profile='lite'] .mh-skeleton::after {
  animation: none;
}
@keyframes mh-shimmer {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(100%);
  }
}

@keyframes mh-island-shimmer {
  0% { transform: translateX(-110%); opacity: 0; }
  20% { opacity: .9; }
  75% { opacity: .3; }
  100% { transform: translateX(110%); opacity: 0; }
}

@keyframes mh-island-icon-pop {
  0% { transform: scale(.72); opacity: .25; }
  70% { transform: scale(1.08); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

.mh-visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.mh-focus-ring:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

/* Island 内部: アイコン領域は中身がある時だけ確保し、ラベルはクロスフェード */
.mh-island-icon {
  position: relative;
  flex: 0 0 18px;
  width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  color: currentColor;
}
.mh-island-icon svg {
  width: 18px;
  height: 18px;
  display: block;
  transition: transform var(--motion-duration-fast) var(--motion-spring);
}
.mh-island-icon .mh-island-glyph {
  transition: opacity var(--motion-duration-fast) var(--motion-ease-standard), transform var(--motion-duration-fast) var(--motion-spring);
  transform-origin: center;
}
.mh-island-icon--dot {}
.mh-island-icon[data-glyph='dot'] .mh-island-glyph--dot,
.mh-island-icon[data-glyph='search'] .mh-island-glyph--search,
.mh-island-icon[data-glyph='navigation'] .mh-island-glyph--navigation,
.mh-island-icon[data-glyph='media'] .mh-island-glyph--media,
.mh-island-icon[data-glyph='sync'] .mh-island-glyph--sync,
.mh-island-icon[data-glyph='download'] .mh-island-glyph--download,
.mh-island-icon[data-glyph='success'] .mh-island-glyph--success,
.mh-island-icon[data-glyph='error'] .mh-island-glyph--error,
.mh-island-icon[data-glyph='warning'] .mh-island-glyph--warning {
  opacity: 1;
  transform: scale(1);
}
.mh-island-icon[data-glyph='success'] .mh-island-glyph--success,
.mh-island-icon[data-glyph='error'] .mh-island-glyph--error,
.mh-island-icon[data-glyph='warning'] .mh-island-glyph--warning {
  animation: mh-island-icon-pop var(--motion-duration-fast) var(--motion-spring) both;
}
.mh-island-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity var(--motion-duration-fast) var(--motion-ease-standard), transform var(--motion-duration-fast) var(--motion-spring);
}
.mh-island-label[data-swap='out'] {
  opacity: 0;
}
.mh-island-label[data-swap='in'] {
  opacity: 1;
}

/* Island の高さぶん、各画面の上端を空ける（時計等が Island の下に潜らないように） */
:root {
  --island-height: 40px;
  --island-clearance: calc(env(safe-area-inset-top, 0px) + var(--space-3) + var(--island-height) + var(--space-3));
}


/* --- Motion refinements --- */
[data-motion-profile='rich'] .mh-tab-indicator {
  box-shadow:
    inset 0 1px 0 color-mix(in srgb, #fff 10%, transparent),
    0 5px 18px color-mix(in srgb, var(--color-accent) 14%, transparent);
}

[data-motion-profile='rich'] .mh-island[data-state='expanded'],
[data-motion-profile='rich'] .mh-island[data-state='reader'],
[data-motion-profile='rich'] .mh-island[data-state='media'] {
  box-shadow:
    0 18px 46px rgb(0 0 0 / 0.34),
    inset 0 1px 0 rgb(255 255 255 / 0.11);
}

[data-reduced-motion='true'] .mh-screen {
  transition: opacity var(--motion-duration-fast) linear;
}

[data-reduced-motion='true'] .mh-tab-indicator,
[data-reduced-motion='true'] .mh-island,
[data-reduced-motion='true'] .mh-island-icon svg,
[data-reduced-motion='true'] .mh-island-label {
  transition-duration: var(--motion-duration-fast) !important;
}

[data-reduced-motion='true'] .mh-island::before,
[data-reduced-motion='true'] .mh-island .mh-island-glyph {
  animation: none !important;
}

@media (max-width: 360px) {
  .mh-tab {
    font-size: 10px;
    padding-inline: 6px;
  }
  .mh-island {
    width: min(56vw, 160px);
  }
  .mh-island[data-state='expanded'],
  .mh-island[data-state='reader'],
  .mh-island[data-state='media'] {
    width: min(88vw, 340px);
  }
}
