:root {
  --primary: #1769e8;
  --primary-dark: #0f4fb8;
  --primary-soft: #eaf2ff;
  --text: #182033;
  --muted: #667085;
  --border: #d8e0ec;
  --surface: #ffffff;
  --bg: #f3f7fc;
  --danger: #b42318;
  --shadow: 0 12px 32px rgba(35, 55, 80, 0.10);
}

* { box-sizing: border-box; }
html { color-scheme: light; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans JP", "Yu Gothic", sans-serif;
  color: var(--text);
  background: linear-gradient(180deg, #f8fbff 0%, var(--bg) 100%);
}
button, input, select { font: inherit; }
button { touch-action: manipulation; }

.app-shell { width: min(1180px, calc(100% - 28px)); margin: 18px auto 0; }
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  font-size: 23px;
  box-shadow: 0 8px 18px rgba(23, 105, 232, 0.25);
}
h1 { margin: 0; font-size: clamp(1.28rem, 2.3vw, 1.85rem); letter-spacing: 0.02em; }
.header-actions { display: flex; gap: 8px; }

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 16px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
  transition: background .18s ease, transform .05s ease, box-shadow .18s ease;
}
.btn:hover { background: var(--primary-dark); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .tab-btn:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid rgba(23, 105, 232, .22);
  outline-offset: 2px;
}
.btn-secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #f7f9fc; }
.btn-outline { background: #fff; color: var(--primary); border-color: var(--primary); }
.btn-outline:hover { background: var(--primary-soft); }
.btn:disabled { opacity: .48; cursor: not-allowed; transform: none; }
.btn-wide { width: 100%; }

.workspace {
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}
.card {
  background: var(--surface);
  border: 1px solid rgba(216, 224, 236, .95);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.control-card { overflow: hidden; }
.control-section { padding: 18px; border-bottom: 1px solid var(--border); }
.control-section:last-child { border-bottom: 0; }
.section-title { margin: 0 0 12px; font-size: 1rem; }

.upload-zone {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 112px;
  padding: 18px;
  border: 2px dashed #8cb6f6;
  border-radius: 12px;
  background: #f8fbff;
  color: var(--primary);
  text-align: center;
  cursor: pointer;
  transition: background .18s ease, border-color .18s ease;
}
.upload-zone:hover, .upload-zone.is-dragover { background: var(--primary-soft); border-color: var(--primary); }
.upload-zone input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.upload-icon { font-size: 30px; line-height: 1; }
.upload-main { font-weight: 800; }
.upload-sub { margin-top: 5px; color: var(--muted); font-size: .82rem; }
.file-name { margin: 10px 0 0; color: var(--muted); font-size: .82rem; overflow-wrap: anywhere; }

.tabs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 5px; padding: 4px; border-radius: 11px; background: #eef2f7; }
.tab-btn {
  min-width: 0;
  border: 0;
  border-radius: 8px;
  padding: 9px 6px;
  background: transparent;
  color: #536174;
  font-weight: 750;
  font-size: .88rem;
  cursor: pointer;
}
.tab-btn.is-active { background: #fff; color: var(--primary); box-shadow: 0 2px 8px rgba(35, 55, 80, .10); }
.mode-panel { padding-top: 14px; }
.mode-panel[hidden] { display: none !important; }

.field { display: grid; gap: 7px; margin-top: 12px; }
.field:first-child { margin-top: 0; }
.field label { color: #344054; font-size: .88rem; font-weight: 700; }
.inline-input { display: grid; grid-template-columns: 1fr auto; align-items: center; }
.inline-input input { border-radius: 9px 0 0 9px; }
.input-suffix { align-self: stretch; display: grid; place-items: center; min-width: 46px; padding: 0 10px; border: 1px solid var(--border); border-left: 0; border-radius: 0 9px 9px 0; background: #f7f9fc; color: var(--muted); }
input[type="number"], select {
  width: 100%;
  min-height: 42px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: #fff;
  color: var(--text);
}
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 999px;
  background: linear-gradient(
    to right,
    var(--primary) 0,
    var(--primary) var(--range-progress, 19.2%),
    #d7e3f4 var(--range-progress, 19.2%),
    #d7e3f4 100%
  );
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -6px;
  border: 0;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 2px 6px rgba(23, 105, 232, 0.28);
}
input[type="range"]::-moz-range-track {
  height: 6px;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(
    to right,
    var(--primary) 0,
    var(--primary) var(--range-progress, 19.2%),
    #d7e3f4 var(--range-progress, 19.2%),
    #d7e3f4 100%
  );
}
input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border: 0;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 2px 6px rgba(23, 105, 232, 0.28);
}
.range-scale {
  position: relative;
  height: 1.2em;
  margin-top: 6px;
  color: var(--muted);
  font-size: .75rem;
}
.range-mark {
  position: absolute;
  top: 0;
  white-space: nowrap;
  transform: translateX(-50%);
}
.range-mark-min {
  left: 0;
  transform: none;
}
.range-mark-mid {
  left: 19.2%;
}
.range-mark-max {
  right: 0;
  left: auto;
  transform: none;
}
.pixel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.lock-line { margin-top: 10px; text-align: center; color: var(--primary); font-size: .82rem; font-weight: 700; }

.summary-box { margin-top: 14px; padding: 13px; border-radius: 11px; background: #f7f9fc; border: 1px solid #e6ebf2; }
.summary-label { color: var(--muted); font-size: .78rem; }
.summary-value { margin-top: 3px; font-size: 1.1rem; font-weight: 850; font-variant-numeric: tabular-nums; }
.estimate-row { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-top: 9px; padding-top: 9px; border-top: 1px solid #e2e8f0; }
.estimate-row strong { color: var(--primary); font-variant-numeric: tabular-nums; }
.estimate-row strong.is-error { color: var(--danger); }

.format-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.quality-wrap[hidden] { display: none !important; }
.action-stack { display: grid; gap: 9px; }

.preview-card { padding: 18px; min-width: 0; }
.preview-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 12px; }
.preview-title { margin: 0; font-size: 1rem; }
.preview-stage {
  position: relative;
  min-height: 480px;
  max-height: 68vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  background:
    linear-gradient(45deg, #edf1f5 25%, transparent 25%),
    linear-gradient(-45deg, #edf1f5 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #edf1f5 75%),
    linear-gradient(-45deg, transparent 75%, #edf1f5 75%), #f8fafc;
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}
.empty-state { position: absolute; inset: 0; display: grid; place-items: center; padding: 30px; text-align: center; color: var(--muted); }
.preview-scroll-inner { min-width: 100%; min-height: 478px; display: grid; place-items: center; padding: 38px; }
.preview-box { position: relative; flex: none; line-height: 0; box-shadow: 0 12px 28px rgba(21, 39, 64, .20); background: #fff; }
.preview-box[hidden] { display: none !important; }
#previewImage { display: block; width: 100%; height: 100%; object-fit: fill; user-select: none; -webkit-user-drag: none; }
.preview-meta { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.meta-item { padding: 10px 12px; border-radius: 10px; background: #f7f9fc; text-align: center; color: var(--muted); font-size: .82rem; }
.meta-item strong { display: block; margin-top: 2px; color: var(--text); font-size: .93rem; font-variant-numeric: tabular-nums; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(16, 24, 40, .55);
}
.modal-backdrop[hidden] { display: none !important; }
.modal {
  width: min(780px, 100%);
  max-height: min(84vh, 760px);
  overflow: auto;
  border-radius: 15px;
  background: #fff;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .28);
}
.modal-head { position: sticky; top: 0; display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 16px 18px; border-bottom: 1px solid var(--border); background: #fff; z-index: 1; }
.modal-head h2 { margin: 0; font-size: 1.2rem; }
.icon-btn { width: 38px; height: 38px; border: 0; border-radius: 50%; background: #eef2f7; color: var(--text); font-size: 1.35rem; cursor: pointer; }
.modal-body { padding: 18px; }
.help-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.help-item { padding: 14px; border: 1px solid var(--border); border-radius: 11px; background: #fbfcfe; }
.help-item h3 { margin: 0 0 7px; font-size: .98rem; color: var(--primary); }
.help-item p { margin: 0; color: #475467; line-height: 1.75; font-size: .9rem; }
.modal-foot { padding: 0 18px 18px; text-align: center; }

.site-footer { width: min(1180px, calc(100% - 28px)); margin: 14px auto 0; padding: 8px 0 16px; text-align: center; color: #1565c0; font-size: .82rem; border-top: 1px solid #dbe6f4; }
.site-footer a { color: #1565c0; text-decoration: none; font-weight: 800; }
.site-footer a:hover { color: #0d47a1; }

.ad-panel { width: min(1180px, calc(100% - 28px)); margin: 12px auto 0; border: 1px solid #d7e6f8; border-radius: 10px; background: #fff; overflow: hidden; }
.ad-panel[hidden] { display: none !important; margin: 0 !important; padding: 0 !important; border: 0 !important; height: 0 !important; }
.ad-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 5px 8px; background: #f4f8ff; border-bottom: 1px solid #d7e6f8; }
.ad-label, .ad-wait { font-size: .72rem; color: #4f6f91; }
.ad-wait { margin-left: auto; }
.ad-close-btn { display: none; margin: 0; padding: 3px 8px; border: 0; border-radius: 999px; background: #1565c0; color: #fff; font-size: .72rem; font-weight: 800; cursor: pointer; }
.ad-close-btn:hover { background: #0d47a1; }
.ad-body { min-height: 90px; padding: 8px; background: #fff; }

.toast { position: fixed; left: 50%; bottom: 22px; z-index: 1100; transform: translateX(-50%) translateY(20px); padding: 10px 16px; border-radius: 999px; background: #172033; color: #fff; opacity: 0; pointer-events: none; transition: opacity .18s ease, transform .18s ease; }
.toast.is-visible { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 820px) {
  .workspace { grid-template-columns: 1fr; }
  .preview-stage { min-height: 360px; max-height: 56vh; }
  .preview-scroll-inner { min-height: 358px; padding: 26px; }
}
@media (max-width: 560px) {
  .app-shell { width: min(100% - 18px, 1180px); margin-top: 10px; }
  .app-header { align-items: flex-start; }
  .brand-mark { width: 38px; height: 38px; }
  .header-actions { flex-direction: column; }
  .header-actions .btn { padding: 8px 11px; font-size: .84rem; }
  .preview-card, .control-section { padding: 13px; }
  .preview-stage { min-height: 300px; }
  .preview-scroll-inner { min-height: 298px; padding: 22px; }
  .pixel-grid, .format-grid, .preview-meta, .help-grid { grid-template-columns: 1fr; }
  .tab-btn { font-size: .78rem; }
}

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