:root {
  --primary: #1565c0;
  --primary-dark: #0d47a1;
  --accent: #00a3d9;
  --bg: #edf4fb;
  --card: #ffffff;
  --text: #263238;
  --muted: #607d8b;
  --border: #d7e6f8;
  --danger: #c62828;
  --shadow: 0 12px 30px rgba(21, 101, 192, 0.12);
}

* { box-sizing: border-box; }
html { min-height: 100%; }
body {
  margin: 0;
  min-height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", sans-serif;
  background: radial-gradient(circle at top left, #ffffff 0, var(--bg) 42%, #e6f2ff 100%);
  color: var(--text);
}

a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }

.app-shell {
  width: min(1120px, calc(100% - 24px));
  margin: 18px auto 0;
  padding-bottom: 16px;
}

.hero {
  background: linear-gradient(135deg, #ffffff, #f7fbff);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 22px;
  text-align: center;
}

h1 {
  margin: 0 0 8px;
  font-size: clamp(1.55rem, 3.4vw, 2.35rem);
  letter-spacing: 0.02em;
}

.lead {
  margin: 0 auto;
  max-width: 820px;
  color: var(--muted);
  line-height: 1.75;
  font-size: 0.98rem;
}

.main-grid {
  margin-top: 16px;
  display: grid;
  grid-template-columns: 330px 1fr;
  gap: 16px;
  align-items: start;
}

.card {
  background: rgba(255,255,255,0.96);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 16px;
}

.card h2 {
  margin: 0 0 12px;
  font-size: 1.05rem;
  color: var(--primary-dark);
}

.drop-zone {
  border: 2px dashed #9cc8ef;
  border-radius: 16px;
  background: #f8fbff;
  padding: 20px 12px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.18s, background-color 0.18s, transform 0.18s;
}

.drop-zone.dragover {
  border-color: var(--primary);
  background: #eaf5ff;
  transform: translateY(-1px);
}

.drop-zone strong { display: block; margin-bottom: 6px; color: var(--primary-dark); }
.drop-zone span { color: var(--muted); font-size: 0.88rem; }
.drop-zone input { display: none; }

.control-group {
  display: grid;
  gap: 12px;
  margin-top: 14px;
}

.control-row {
  display: grid;
  gap: 6px;
}

.control-row label {
  font-size: 0.88rem;
  color: #37546b;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.control-row input[type="range"],
.control-row select {
  width: 100%;
}

.control-row select,
button {
  font: inherit;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

button {
  border: none;
  border-radius: 999px;
  padding: 10px 14px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  font-weight: 700;
  transition: background-color 0.18s, transform 0.04s, opacity 0.18s;
}

button:hover { background: var(--primary-dark); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.48; cursor: not-allowed; }

.btn-secondary { background: #607d8b; }
.btn-secondary:hover { background: #455a64; }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #8e0000; }
.btn-light {
  background: #e8f2fb;
  color: var(--primary-dark);
}
.btn-light:hover { background: #d7eaf9; }

.status {
  min-height: 1.5em;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.55;
  margin-top: 12px;
}

.preview-panel { min-width: 0; }
.canvas-toolbar {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 999px;
  background: #e8f3ff;
  color: var(--primary-dark);
  padding: 6px 10px;
  font-size: 0.82rem;
  font-weight: 700;
}

.manual-active .badge.manual {
  background: #fff4d8;
  color: #795300;
}

.canvas-wrap {
  position: relative;
  border-radius: 18px;
  overflow: hidden;
  background: repeating-linear-gradient(45deg, #eef4fa, #eef4fa 12px, #f7fbff 12px, #f7fbff 24px);
  border: 1px solid var(--border);
  min-height: 320px;
  display: grid;
  place-items: center;
}

#workCanvas {
  display: block;
  max-width: 100%;
  height: auto;
  touch-action: none;
}

.empty-message {
  color: #6d879a;
  text-align: center;
  padding: 28px;
  line-height: 1.75;
}

.results {
  margin-top: 16px;
}

.result-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.result-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.crop-card {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #ffffff;
  overflow: hidden;
  box-shadow: 0 6px 16px rgba(38,50,56,0.07);
}

.crop-thumb {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #f3f7fb;
  display: block;
  object-fit: contain;
  border-bottom: 1px solid var(--border);
}

.crop-body {
  padding: 10px;
  display: grid;
  gap: 8px;
}

.crop-title {
  color: var(--primary-dark);
  font-weight: 800;
  font-size: 0.9rem;
}

.crop-meta {
  color: var(--muted);
  font-size: 0.78rem;
}

.crop-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.crop-actions button {
  padding: 7px 10px;
  font-size: 0.82rem;
}

.help {
  margin-top: 12px;
  border-left: 4px solid #9cc8ef;
  background: #f7fbff;
  padding: 10px 12px;
  border-radius: 10px;
  color: #48677d;
  font-size: 0.86rem;
  line-height: 1.7;
}

.app-footer {
  margin: 14px auto 0;
  padding: 10px 0 14px;
  text-align: center;
  color: #1565c0;
  font-size: 0.88rem;
  border-top: 1px solid rgba(21,101,192,0.16);
}

.app-footer a {
  color: #1565c0;
  font-weight: 700;
}

.app-footer a:hover { color: #0d47a1; }

.ad-panel {
  max-width: 880px;
  margin: 12px auto 0;
  padding: 0;
  border: 1px solid #d7e6f8;
  border-radius: 10px;
  background: #ffffff;
  overflow: hidden;
}

.ad-panel[hidden] {
  display: none !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  height: 0 !important;
  overflow: hidden !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 {
  font-size: 0.72rem;
  color: #4f6f91;
  line-height: 1;
}

.ad-wait-message {
  margin-left: auto;
  font-size: 0.72rem;
  color: #4f6f91;
}

.ad-close-btn {
  margin: 0 !important;
  padding: 3px 8px !important;
  min-height: 0;
  height: auto;
  line-height: 1.2;
  border: none;
  border-radius: 999px;
  background: #1565c0;
  color: #ffffff;
  font-size: 0.72rem !important;
  font-weight: 700;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
}

.ad-close-btn:hover { background: #0d47a1; }
.ad-close-btn[hidden] { display: none !important; }

.ad-body {
  padding: 8px;
  background: #ffffff;
  min-height: 80px;
}

@media (max-width: 840px) {
  .main-grid { grid-template-columns: 1fr; }
  .app-shell { width: min(100% - 14px, 1120px); margin-top: 8px; }
  .hero, .card { border-radius: 14px; padding: 14px; }
  .canvas-wrap { min-height: 250px; }
  .button-row button { flex: 1 1 auto; }
}
