/**
 * Admin "Edit map" page (post type: w2dc_map) - custom layout.
 * Form-builder-style: title + Save button at top, sticky live map preview on the right,
 * compact metaboxes on the left.
 */

/* ===== Top toolbar (replaces native title input + side Publish metabox actions) ===== */
.w2dc-map-toolbar {
	display: flex;
	align-items: center;
	gap: 12px;
	padding: 10px 14px;
	margin: 0 0 16px;
	background: #fff;
	border: 1px solid #c3c4c7;
	border-radius: 4px;
	position: sticky;
	top: 32px; /* WP admin bar */
	z-index: 99;
	box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.w2dc-map-toolbar-left {
	display: flex;
	align-items: end;
	gap: 12px;
	flex-shrink: 0;
}
.w2dc-map-toolbar-heading {
	margin: 0;
	font-size: 20px;
	font-weight: 600;
	color: #1d2327;
	line-height: 1.2;
	padding: 0;
}
.w2dc-map-toolbar-newbtn {
	font-size: 13px;
	padding: 4px 10px;
	border: 1px solid var(--w2dc-primary, #2271b1);
	color: var(--w2dc-primary, #2271b1);
	background: #fff;
	border-radius: 4px;
	text-decoration: none;
	white-space: nowrap;
	transition: background 0.15s, color 0.15s;
}
.w2dc-map-toolbar-newbtn:hover,
.w2dc-map-toolbar-newbtn:focus {
	background: var(--w2dc-primary, #2271b1);
	color: #fff;
	text-decoration: none;
}
/* Title + slug group - fills the flex space between left chrome
 * (heading + Create-new) and right chrome (Undo/Redo/Save/etc.).
 * Title input stretches, slug is a fixed narrower column. */
.w2dc-map-toolbar-titlegroup {
	flex: 1;
	display: flex;
	align-items: center;
	gap: 8px;
	min-width: 0;
}
.w2dc-map-toolbar .w2dc-map-title-input {
	flex: 1;
	font-size: 16px;
	font-weight: 500;
	padding: 6px 10px;
	border: 1px solid #dcdcde;
	border-radius: 4px;
	min-width: 0;
}
.w2dc-map-toolbar .w2dc-map-slug-input {
	flex: 0 0 220px;
	font-size: 13px;
	font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
	padding: 6px 10px;
	border: 1px solid #dcdcde;
	border-radius: 4px;
	color: #6b7280;
	background: #f9fafb;
	min-width: 0;
}
.w2dc-map-toolbar .w2dc-map-title-input:focus,
.w2dc-map-toolbar .w2dc-map-slug-input:focus {
	border-color: #2271b1;
	box-shadow: 0 0 0 1px #2271b1;
	outline: none;
	background: #fff;
	color: #1f2937;
}
.w2dc-map-toolbar .w2dc-map-slug-input:invalid {
	border-color: #dc2626;
	box-shadow: 0 0 0 1px #dc2626;
}
.w2dc-map-toolbar-right {
	display: flex;
	align-items: center;
	gap: 8px;
	flex-shrink: 0;
}
/* Undo / Redo - form-builder style:
 * px-3 py-1.5 rounded border-gray-300 bg-white text-gray-700 hover:bg-gray-50 disabled:opacity-40 */
.w2dc-map-toolbar .w2dc-map-undo-btn,
.w2dc-map-toolbar .w2dc-map-redo-btn {
	padding: 6px 12px;
	font-size: 13px;
	background: #fff;
	color: #374151;
	border: 1px solid #d1d5db;
	border-radius: 4px;
	cursor: pointer;
	white-space: nowrap;
	transition: background 0.15s;
}
.w2dc-map-toolbar .w2dc-map-undo-btn:hover:not(:disabled),
.w2dc-map-toolbar .w2dc-map-redo-btn:hover:not(:disabled) {
	background: #f9fafb;
}
.w2dc-map-toolbar .w2dc-map-undo-btn:disabled,
.w2dc-map-toolbar .w2dc-map-redo-btn:disabled {
	opacity: 0.4;
	cursor: not-allowed;
}

/* Export - dev-only ghost button sized to match Undo/Redo. The
 * `disabled` state keeps the same styling as Undo/Redo so admins
 * without WP_DEBUG see a consistent "not for you" affordance and
 * the tooltip explains why. */
.w2dc-map-toolbar .w2dc-map-export-btn {
	padding: 5px 10px;
	font-size: 13px;
	background: #fff;
	color: #64748b;
	border: 1px solid #d1d5db;
	border-radius: 4px;
	cursor: pointer;
	white-space: nowrap;
	transition: background 0.15s, color 0.15s;
}
.w2dc-map-toolbar .w2dc-map-export-btn:hover:not(:disabled) {
	background: #f9fafb;
	color: #334155;
}
.w2dc-map-toolbar .w2dc-map-export-btn:disabled {
	opacity: 0.4;
	cursor: not-allowed;
}

/* Save - primary action button. Reads the admin's saved button
 * palette via the CSS variables that `classes/admin/admin.php`
 * injects on the page (`--w2dc-btn1` / `--w2dc-btn2` /
 * `--w2dc-btn-text` from `w2dc_button_1_color` /
 * `w2dc_button_2_color` / `w2dc_button_text_color`). Hex fallbacks
 * preserve the pre-rebrand look when the variables aren't set. */
.w2dc-map-toolbar .w2dc-map-save-btn {
	padding: 6px 16px;
	font-size: 13px;
	font-weight: 500;
	background: var(--w2dc-btn1, #2563eb);
	color: var(--w2dc-btn-text, #fff);
	border: 1px solid var(--w2dc-btn1, #2563eb);
	border-radius: 4px;
	cursor: pointer;
	white-space: nowrap;
	transition: background 0.15s, border-color 0.15s;
}
.w2dc-map-toolbar .w2dc-map-save-btn:hover:not(:disabled) {
	background: var(--w2dc-btn2, #1d4ed8);
	border-color: var(--w2dc-btn2, #1d4ed8);
}
.w2dc-map-toolbar .w2dc-map-save-btn:disabled {
	opacity: 0.4;
	cursor: not-allowed;
}

/* "Create page with this map" - outlined sibling of Save. Same
 * palette so the two buttons read as a pair (outline vs filled).
 * Hover keeps the outline visible by lifting the border to btn2. */
.w2dc-map-toolbar .w2dc-map-create-page-btn {
	display: inline-flex;
	align-items: center;
	padding: 5px 14px;
	font-size: 13px;
	font-weight: 500;
	background: #fff;
	color: var(--w2dc-btn1, #2563eb);
	border: 1px solid var(--w2dc-btn1, #2563eb);
	border-radius: 4px;
	text-decoration: none;
	white-space: nowrap;
	transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.w2dc-map-toolbar .w2dc-map-create-page-btn:hover {
	background: color-mix(in srgb, var(--w2dc-btn1, #2563eb) 8%, #fff);
	color: var(--w2dc-btn2, #1d4ed8);
	border-color: var(--w2dc-btn2, #1d4ed8);
}
.w2dc-map-toolbar .w2dc-map-create-page-btn[hidden] {
	display: none;
}
/* "Open page with this map" variant - rendered when a page with this
 * map's shortcode already exists. Same shape as the Create button so
 * the toolbar layout doesn't shift, but a softer green palette so the
 * user reads "this opens an existing thing" instead of "this creates
 * a new thing". The CSS class is added by `render_map_edit_toolbar()`
 * when the duplicate-prevention helper finds an existing page. */
.w2dc-map-toolbar .w2dc-map-create-page-btn--existing {
	color: #15803d;
	border-color: #15803d;
}
.w2dc-map-toolbar .w2dc-map-create-page-btn--existing:hover {
	background: #f0fdf4;
	color: #166534;
	border-color: #166534;
}

/* Toast - green-100/red-100 background w/ matching border.
 * Floated out of flow (position:absolute) below the toolbar so a
 * multi-line export summary (one written file path per line) doesn't
 * stretch the toolbar row or push the buttons. The toolbar is
 * `position: sticky` (non-static) so it's the containing block for
 * this absolute child. `white-space: pre-line` honours the `\n`
 * line breaks the export handler writes; `word-break` wraps the long
 * absolute file paths instead of overflowing. */
.w2dc-map-toolbar .w2dc-map-toast {
	position: absolute;
	top: calc(100% + 6px);
	right: 0;
	z-index: 100;
	max-width: min(680px, 96vw);
	padding: 8px 12px;
	font-size: 13px;
	line-height: 1.5;
	border-radius: 4px;
	white-space: pre-line;
	word-break: break-all;
	text-align: left;
	background: #dcfce7;
	color: #15803d;
	border: 1px solid #86efac;
	box-shadow: 0 4px 12px rgba(0,0,0,0.12);
	transition: opacity 0.2s;
}
.w2dc-map-toolbar .w2dc-map-toast.w2dc-map-toast--error {
	background: #fee2e2;
	color: #b91c1c;
	border-color: #fca5a5;
}
.w2dc-map-toolbar .w2dc-map-toast[hidden] {
	display: none !important;
}

/* Hide WP elements we replaced */
body.w2dc-map-edit #titlediv,
body.w2dc-map-edit #postbox-container-1,                /* right sidebar (Publish, Categories, etc.) */
body.w2dc-map-edit #screen-meta,                         /* Screen Options panel */
body.w2dc-map-edit #screen-meta-links,
body.w2dc-map-edit #post-status-info,
body.w2dc-map-edit #message,
body.w2dc-map-edit h1.wp-heading-inline,                 /* "Edit map" / "Add new map" heading */
body.w2dc-map-edit .page-title-action,                   /* "Add new" link near heading */
body.w2dc-map-edit .wrap > hr.wp-header-end,
/* Inline map appended into starting-point metabox by google_maps.js - replaced by our right-side preview */
body.w2dc-map-edit #w2dc-starting-point-metabox-map-canvas {
	display: none !important;
}

/* ===== Two-column workspace: metaboxes left, sticky map right ===== */
body.w2dc-map-edit #poststuff {
	padding-top: 0;
}
body.w2dc-map-edit #post-body.columns-2,
body.w2dc-map-edit #post-body {
	display: flex;
	gap: 16px;
	align-items: flex-start;
	margin: 0 !important;       /* kill WP's default right margin reserved for the sidebar */
	padding: 0 !important;
}
/* Right column (live preview lives inside #post-body-content via edit_form_after_editor hook).
 * WP load-styles.php applies width:100%, min-width:463px, float:left to #post-body-content -
 * we override with !important since they have higher specificity in the cascade order.
 * Width is driven by --w2dc-map-w (set by the resize handle, persisted to
 * localStorage); default 40%, clamped 40-60% by the JS drag handler. */
body.w2dc-map-edit #post-body-content {
	order: 3;
	flex: 0 0 var(--w2dc-map-w, 40%) !important;
	width: var(--w2dc-map-w, 40%) !important;
	min-width: 0 !important;
	max-width: var(--w2dc-map-w, 40%) !important;
	float: none !important;
	margin: 0 !important;
	padding: 0 !important;
	position: sticky;
	top: 92px;            /* admin bar (32) + toolbar (~50) + spacing */
	height: calc(100vh - 110px);
	display: flex;
	flex-direction: column;
}

/* Resize bar between the metaboxes (order: 1) and the live preview
 * (order: 3). Mirrors the form-builder's right-rail handle: a thin
 * vertical strip that highlights on hover, drags horizontally to
 * change the live preview's width via --w2dc-map-w. */
body.w2dc-map-edit .w2dc-map-resize-handle {
	order: 2;
	flex: 0 0 6px;
	align-self: stretch;
	/* Visible at rest so users discover the affordance without
	 * hovering - same neutral grey the postbox borders use, then
	 * brighter primary-tint on hover/drag. */
	background: #c3c4c7;
	cursor: col-resize;
	position: sticky;
	top: 92px;
	height: calc(100vh - 110px);
	transition: background-color 0.12s;
}
body.w2dc-map-edit .w2dc-map-resize-handle:hover,
body.w2dc-map-edit .w2dc-map-resize-handle.is-dragging {
	background: rgba(37, 99, 235, 0.55);
}
/* Left column - metaboxes */
body.w2dc-map-edit #postbox-container-2 {
	order: 1;
	flex: 1 1 auto;
	min-width: 0;
	float: none;
	width: auto !important;
	margin: 0 !important;
}

/* (Starting-point InfoWindow CSS removed - popup is now part of the marker via custom OverlayView,
 * see react/src/admin-map-edit/StartingPointOverlay.tsx + admin-map-edit.css.) */

/* Live preview panel */
.w2dc-map-preview {
	background: #f3f4f6;
	border: 1px solid #c3c4c7;
	border-radius: 4px;
	overflow: hidden;
	display: flex;
	flex-direction: column;
	flex: 1 1 auto;
	min-height: 0;
}
.w2dc-map-preview-canvas {
	flex: 1 1 auto;
	min-height: 0;
}

/* Compact metaboxes on the left */
body.w2dc-map-edit .postbox {
	margin-bottom: 12px;
}
body.w2dc-map-edit .postbox .inside {
	padding: 10px 12px;
}

/* ===== Responsive breakpoints ===== */

/* ≤1023px - toolbar only: the top "Edit map" toolbar's three flex groups
 * (left heading + new btn, middle title input, right action buttons)
 * overflow on narrow widths, truncating the title input and clipping the
 * last action. Reflow into three stacked rows here, and DROP the sticky
 * positioning (the wrapped toolbar gets tall and a sticky tall bar eats
 * the preview area).
 *
 * Body / metabox layout intentionally NOT in this block - keep the
 * intermediate 30%-narrow map step (1023px below) and the full vertical-
 * stack step (850px further down) as separate breakpoints. */
@media (max-width: 1023px) {
	.w2dc-map-toolbar {
		flex-wrap: wrap;
		gap: 8px;
		position: static;
		top: auto;
	}
	.w2dc-map-toolbar-left {
		flex: 1 1 100%;
		justify-content: space-between;
		align-items: center;
	}
	.w2dc-map-toolbar .w2dc-map-title-input {
		flex: 1 1 100%;
		min-width: 0;
	}
	.w2dc-map-toolbar-right {
		flex: 1 1 100%;
		flex-wrap: wrap;
		justify-content: flex-end;
	}
}

/* 851-1023px: map narrower (30%, drag-disabled below this breakpoint). */
@media (max-width: 1023px) {
	body.w2dc-map-edit #post-body-content,
	body.w2dc-map-edit #post-body.columns-2 #post-body-content {
		flex: 0 0 30% !important;
		width: 30% !important;
		max-width: 30% !important;
	}
	body.w2dc-map-edit .w2dc-map-resize-handle { display: none; }
}

/* < 850px: map on top, full width, height = Map height setting, non-sticky.
 *
 * `min-height` (not `height`) so the post-body-content can GROW past the
 * configured map height when the in-map sidebar reflows into a bottom
 * drawer on narrow viewports - the React wrapper inside switches to
 * `height: auto` and stacks the form + canvas-host + drawer. A fixed
 * `height` here would let `.w2dc-map-preview { overflow: hidden }`
 * clip the bottom of the drawer, hiding the sidebar entirely. The
 * preview also gets `overflow: visible` on mobile for the same reason. */
@media (max-width: 850px) {
	body.w2dc-map-edit #post-body.columns-2,
	body.w2dc-map-edit #post-body {
		flex-direction: column;
	}
	body.w2dc-map-edit #post-body-content,
	body.w2dc-map-edit #post-body.columns-2 #post-body-content {
		order: 0 !important;
		position: static !important;
		flex: 0 0 auto !important;
		max-width: 100% !important;
		width: 100% !important;
		height: auto !important;
		min-height: var(--w2dc-map-preview-h, 400px) !important;
		margin: 0 0 12px 0 !important;
	}
	body.w2dc-map-edit .w2dc-map-preview {
		overflow: visible !important;
	}
}
