*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg-deep: #0f0f1a;
  --bg-base: #1a1a2e;
  --bg-surface: #16213e;
  --bg-card: #1c2541;
  --text-primary: #e0dfd5;
  --text-secondary: #8b8a80;
  --text-dim: #55546e;
  --border-subtle: rgba(255, 255, 255, 0.06);
  --border-glow: rgba(255, 255, 255, 0.1);
  --nav-height: 48px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html {
  height: 100%;
  overflow: hidden;
}

body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, 'Hiragino Sans', system-ui, sans-serif;
  background: var(--bg-deep);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  user-select: none;
  -webkit-user-select: none;
}

/* ── Background atmosphere ── */
.bg-atmosphere {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse 80% 50% at 50% 0%, rgba(30, 40, 80, 0.6) 0%, transparent 60%),
    radial-gradient(ellipse 60% 40% at 20% 100%, rgba(40, 20, 60, 0.3) 0%, transparent 50%),
    radial-gradient(ellipse 50% 30% at 80% 80%, rgba(20, 50, 60, 0.25) 0%, transparent 50%);
}

/* ── Screens ── */
.screen {
  display: none;
  height: 100%;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

/* ── Home screen ── */
#home-screen {
  overflow: hidden;
}

.home-header {
  flex-shrink: 0;
  padding: calc(var(--safe-top) + 20px) 20px 12px;
  position: relative;
  z-index: 1;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
}

.home-title {
  font-family: -apple-system, 'Hiragino Sans', system-ui, sans-serif;
  font-weight: 900;
  font-size: 22px;
  letter-spacing: 0.04em;
  color: var(--text-primary);
  position: relative;
  display: inline-block;
}

.home-title::after {
  content: '';
  position: absolute;
  bottom: -4px;
  left: 0;
  width: 100%;
  height: 2px;
  background: linear-gradient(90deg, rgba(200, 180, 130, 0.5), transparent);
}

.home-subtitle {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 8px;
  letter-spacing: 0.12em;
  font-weight: 400;
}

/* ── Card grid ── */
.tool-grid {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 16px calc(var(--safe-bottom) + 16px);
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  align-content: start;
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
}

/* ── Tool card ── */
.tool-card {
  position: relative;
  border-radius: 14px;
  padding: 20px 16px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  overflow: hidden;
  aspect-ratio: 1 / 0.9;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.tool-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--card-accent);
  opacity: 0.7;
  transition: opacity 0.2s ease;
}

.tool-card::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 14px;
  background: radial-gradient(
    ellipse 100% 80% at 50% -10%,
    rgba(255, 255, 255, 0.04),
    transparent 60%
  );
  pointer-events: none;
}

.tool-card:active {
  transform: scale(0.96);
  box-shadow: 0 0 20px -4px rgba(255, 255, 255, 0.1);
}

.tool-card:active::before {
  opacity: 1;
}

.card-icon {
  font-size: 28px;
  line-height: 1;
  filter: saturate(0.85);
}

.card-body {
  position: relative;
  z-index: 1;
}

.card-name {
  font-family: -apple-system, 'Hiragino Sans', system-ui, sans-serif;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.02em;
  margin-bottom: 3px;
}

.card-desc {
  font-size: 11px;
  color: var(--text-secondary);
  letter-spacing: 0.01em;
  line-height: 1.4;
}

.card-link-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.06);
  z-index: 2;
}

.card-link-badge svg {
  width: 12px;
  height: 12px;
  stroke: var(--text-secondary);
  stroke-width: 2;
  fill: none;
}

/* ── Stagger animation ── */
.tool-card {
  opacity: 0;
  transform: translateY(12px);
  animation: card-enter 0.35s ease forwards;
}

@keyframes card-enter {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ── Tool viewer screen ── */
#tool-screen {
  background: var(--bg-deep);
}

.tool-nav {
  flex-shrink: 0;
  height: calc(var(--nav-height) + var(--safe-top));
  padding-top: var(--safe-top);
  display: flex;
  align-items: center;
  gap: 12px;
  padding-left: max(12px, var(--safe-left));
  padding-right: max(12px, var(--safe-right));
  background: var(--bg-base);
  border-bottom: 1px solid var(--border-subtle);
  position: relative;
  z-index: 10;
}

.nav-back {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  cursor: pointer;
  border-radius: 8px;
  color: var(--text-primary);
  transition: background 0.15s ease;
}

.nav-back:active {
  background: rgba(255, 255, 255, 0.08);
}

.nav-back svg {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  stroke-width: 2.2;
  fill: none;
}

.nav-title {
  font-family: -apple-system, 'Hiragino Sans', system-ui, sans-serif;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.nav-external {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  cursor: pointer;
  border-radius: 8px;
  color: var(--text-secondary);
  transition: background 0.15s ease;
}

.nav-external:active {
  background: rgba(255, 255, 255, 0.08);
}

.nav-external svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  stroke-width: 2;
  fill: none;
}

/* ── iframe container ── */
.iframe-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.iframe-wrap iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: none;
  background: var(--bg-deep);
}

/* ── Loading state ── */
.iframe-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 5;
  background: var(--bg-deep);
  transition: opacity 0.3s ease;
}

.iframe-loading.hidden {
  opacity: 0;
  pointer-events: none;
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 2.5px solid var(--border-glow);
  border-top-color: var(--text-secondary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-text {
  font-size: 12px;
  color: var(--text-dim);
  letter-spacing: 0.08em;
}

/* ── Fallback state ── */
.iframe-fallback {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 32px;
  z-index: 6;
  background: var(--bg-deep);
  text-align: center;
}

.iframe-fallback.visible {
  display: flex;
}

.fallback-icon {
  font-size: 32px;
  opacity: 0.7;
}

.fallback-message {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.fallback-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 24px;
  border-radius: 10px;
  border: 1px solid var(--border-glow);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s ease;
}

.fallback-btn:active {
  background: rgba(255, 255, 255, 0.1);
}

.fallback-btn svg {
  width: 16px;
  height: 16px;
  stroke: currentColor;
  stroke-width: 2;
  fill: none;
}
